自定义Spinner
java逻辑代码:
/* parity */ paritySpinner = (Spinner) findViewById(R.id.parityValue); ArrayAdapter<CharSequence> parityAdapter = ArrayAdapter.createFromResource(this, R.array.parity, R.layout.my_spinner_textview); parityAdapter.setDropDownViewResource(R.layout.my_spinner_textview); paritySpinner.setAdapter(parityAdapter); paritySpinner.setGravity(0x11); /* default is none */ parity = 0;
监听:
/* set the adapter listeners for parity */ paritySpinner.setOnItemSelectedListener(new MyOnParitySelectedListener());
public class MyOnParitySelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { String parityString = new String(parent.getItemAtPosition(pos).toString()); if (parityString.compareTo("None") == 0) { parity = 0; } if (parityString.compareTo("Odd") == 0) { parity = 1; } if (parityString.compareTo("Even") == 0) { parity = 2; } if (parityString.compareTo("Mark") == 0) { parity = 3; } if (parityString.compareTo("Space") == 0) { parity = 4; } } public void onNothingSelected(AdapterView<?> parent) { // Do nothing. }} } }
布局文件Spinner:R.id.parityValue
<Spinner android:id="@+id/parityValue" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="5dp" android:layout_weight="1" android:background="#708070" android:gravity="center_horizontal|center" android:inputType="none" />
下拉布局:R.layout.my_spinner_textview
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" style="?android:attr/spinnerItemStyle" android:layout_width="fill_parent" android:layout_height="35dp" android:gravity="center" />
数据源:R.array.parity
<string-array name="parity"> <item>None</item> <item>Odd</item> <item>Even</item> <item >Mark</item> <item >Space</item> </string-array>
效果图:
这种样式需要设置:
android:spinnerMode="dropdown"
默认:
android:spinnerMode="dialog"