Android 自定义下拉框的实现 Spinner

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 自定义下拉框的实现 Spinner相关的知识,希望对你有一定的参考价值。

 

 

下拉框布局  a_spinner_checked_text.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="8dp"
android:singleLine="true"
android:textColor="#00f"
android:textSize="18sp" >

</CheckedTextView>

下拉框内容布局  a_spinner_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:padding="8dp"
>

<TextView
android:id="@+id/spinner_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textColor="#00f"
android:textSize="14sp" />

<ImageView
android:id="@+id/spinner_item_checked_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/a_check_unselect" />

</RelativeLayout>

 

首页布局 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.spinner.MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:layout_margin="15dp">

<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="center"
android:text="民族"
android:textColor="#000"
android:textSize="18sp" />

<Spinner
android:id="@+id/add_minzsp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1" />
</LinearLayout>

</LinearLayout>

 

MainActivity 代码

public class MainActivity extends Activity {
private Spinner minzuSpinner;

private String[] minzuStrings = { "请选择", "汉族", "维吾尔族", "回族", "哈萨克族", "蒙古族",
"柯尔克孜族", "塔吉克族", "壮族", "乌孜别克族" };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

minzuSpinner = (Spinner) findViewById(R.id.add_minzsp);
SpinnerAdapter adapter = new SpinnerAdapter(this,
R.layout.a_spinner_checked_text, minzuStrings, minzuSpinner);
adapter.setDropDownViewResource(R.layout.a_spinner_item_layout);
minzuSpinner.setAdapter(adapter);


}


}

用到的SpinnerAdapter代码

public class SpinnerAdapter extends ArrayAdapter<String> {

Context context;
String[] items = new String[] {};
Spinner spinner;

public SpinnerAdapter(final Context context,
final int textViewResourceId, final String[] objects,final Spinner spinner) {
super(context, textViewResourceId, objects);
this.items = objects;
this.context = context;
this.spinner=spinner;
}

@Override

public View getDropDownView(int position, View convertView,ViewGroup parent) {

if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(
R.layout.a_spinner_item_layout, parent, false);
}
TextView label = (TextView) convertView.findViewById(R.id.spinner_item_label);
ImageView check = (ImageView) convertView.findViewById(R.id.spinner_item_checked_image);
label.setText(items[position]);
if (spinner.getSelectedItemPosition() == position) {
convertView.setBackgroundColor(context.getResources().getColor(R.color.spinner_green));
check.setImageResource(R.drawable.a_check_select);
} else {
convertView.setBackgroundColor(context.getResources().getColor(R.color.spinner_light_green));
check.setImageResource(R.drawable.a_check_unselect);
}
return convertView;
}


}

 

结果如图:

技术分享

 

以上是关于Android 自定义下拉框的实现 Spinner的主要内容,如果未能解决你的问题,请参考以下文章

Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现) .

android的spinner控件能不能改成只有一个向下的箭头

如何打造Android自定义的下拉列表框控件

Android Spinner实现下拉框几种方式及配合BaseAdapter实现自定义布局

自定义Spinner实现选项换行/多行

如何在 Android 中自定义 Spinner