如何在android.widget.Spininer中定义我自己的样式

时间:2017-02-13 06:45:40

标签: android layout styles spinner

我想在imageview的下拉列表中显示textviewSpinner

,例如[ ImageView(the image of Mr.Green) , TextView(the name of Mr.Green) ]. i

哈瓦很少考虑这个问题,但我无法做到。非常感谢你!

1 个答案:

答案 0 :(得分:0)

使用text和imageview创建自定义适配器:

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>

希望这会有所帮助。 :)