上代码:
spinner = (Spinner) findViewById(R.id.spinner);
tv = (TextView) findViewById(R.id.tv);
final ArrayList<String> list = new ArrayList<>();
list.add("不知道");
list.add("A");
list.add("B");
list.add("AB");
list.add("O"); //这里的R.Layout.spinner_item为spinner的样式
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.spinner_item); //这里的R.layout.spinner_item_item为下拉框textview的样式
adapter.setDropDownViewResource(R.layout.spinner_item_item);
adapter.addAll(list);
spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
tv.setText("你的血型:"+list.get(i)); } @Override
public void onNothingSelected(AdapterView<?> adapterView) { }
});
spinner.setSelection(0); } } xml:R.layout.spinner_item:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:singleLine="true"
android:background="#5888"
android:gravity="center"
android:layout_height="33dp"> </CheckedTextView> xml:R.layout.spinner_item_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#3999"
android:layout_marginTop="2dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="33dp"> </TextView> demo下载地址:https://github.com/renjiemei1225/SpinnerTest