下拉框布局 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;
}
}
结果如图: