我以编程方式创建了一个微调器:
ArrayAdapter<ServiceObject> medicineArrayAdapter = new ArrayAdapter<ServiceObject>(MedicineActivity.this, android.R.layout.simple_spinner_dropdown_item, medicines);
medicineArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = new Spinner(MedicineActivity.this, getSpinnerAttrs(), 0);
spinner.setAdapter(medicineArrayAdapter);
spinner.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
spinner.setVisibility(View.VISIBLE);
buttonLayout.addView(spinner, index);
其中 getSpinnerAttrs():
AttributeSet as = null;
Resources r = getResources();
XmlResourceParser parser = r.getLayout(R.layout.fragment_medicine);
int state = 0;
do {
try {
state = parser.next();
} catch (XmlPullParserException xppe) {
xppe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (state == XmlPullParser.START_TAG) {
if (parser.getName().equals("Spinner")) {
as = Xml.asAttributeSet(parser);
break;
}
}
} while(state != XmlPullParser.END_DOCUMENT);
return as;
fragment_medicine 中的 Spinner 是:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"
android:visibility="invisible"/>
/>
正常显示 但是点击后打不开 . .