我有一个微调器,我想在微调器的末尾添加一个“取消”按钮,所以当用户点击它时,微调器中的选定元素不会改变。
<Spinner
android:id="@+id/s_signin_type"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginTop="5dip"
android:prompt="@string/s_type" />
Spinner s_type;
s_type = (Spinner) findViewById(R.id.s_signin_type);
ArrayAdapter<String> adapterType = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, Data.types);
adapterType
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_type.setAdapter(adapterType);
非常感谢。
答案 0 :(得分:1)
我设法通过覆盖ArrayAdapter中的getDropDownView()方法向微调器添加按钮。它是这样的(我省略了视图重用,......):
List<String> items = new ArrayList<String>();
//items I want to display
items.add("1");
items.add("2");
//add placeholder for button
items.add(null);
...
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, items) {
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
//create view for placeholder
if (getItem(position) == null) {
Button b = new Button(getActivity());
b.setText("Test");
return b;
}
//create view for standard spinner item
return LayoutInflater.from(this.getContext()).inflate(
android.R.layout.simple_spinner_dropdown_item, parent,
false);
}