我以编程方式创建了一个微调器:
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"/>
/>
它显示正常但是当我点击它时,它不会打开。 。
答案 0 :(得分:1)
最后我以另一种方式做到了。
我只是以编程方式添加了属性,而没有从xml文件中读取它们:
spinner.setBackgroundResource(android.R.drawable.btn_dropdown);
它完美无缺。