【发布时间】:2017-07-01 02:03:31
【问题描述】:
我想在Spinner 的下拉列表中显示imageview 和textview。
如[ ImageView(the image of Mr.Green) , TextView(the name of Mr.Green) ].i
对这个问题有点思考,但我做不到。非常感谢!
【问题讨论】:
标签: android layout styles spinner
我想在Spinner 的下拉列表中显示imageview 和textview。
如[ ImageView(the image of Mr.Green) , TextView(the name of Mr.Green) ].i
对这个问题有点思考,但我做不到。非常感谢!
【问题讨论】:
标签: android layout styles spinner
使用文本和图像视图创建自定义适配器:
public class CustomSpinnerAdapter extends BaseAdapter {
Context context;
int image[];
String[] text;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] image, String[] text) {
this.context = applicationContext;
this.image = image;
this.text = text;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return flags.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.spinner_item_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.spImageView);
TextView names = (TextView) view.findViewById(R.id.spTextView);
icon.setImageResource(image[i]);
names.setText(text[i]);
return view;
}
}
然后将此适配器设置为微调器:
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, YourImageArray, YourTextArray);
spinner.setAdapter(adapter);
这是每个微调器项的布局 (spinner_item_layout.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/spTextView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/spImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"/>"
</LinearLayout>
希望这会有所帮助。 :)
【讨论】: