【问题标题】:how to define my-own style in android.widget.Spininer如何在 android.widget.Spinner 中定义我自己的样式
【发布时间】:2017-07-01 02:03:31
【问题描述】:

我想在Spinner 的下拉列表中显示imageviewtextview

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

对这个问题有点思考,但我做不到。非常感谢!

【问题讨论】:

    标签: android layout styles spinner


    【解决方案1】:

    使用文本和图像视图创建自定义适配器:

    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>
    

    希望这会有所帮助。 :)

    【讨论】:

    • 我是新手,所以,layout:spinner_value_layout和spinner_item_layout有什么区别。换句话说,spinner_value_layout怎么写,这个layout是什么?我很困惑。 .....
    • 我已经编辑了答案,这比以前简单了。请给我反馈。
    • 是的,我做到了。它很有用。因为这个网站的机制,我不能给你投票,直到我获得 15 名声望。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    相关资源
    最近更新 更多