完全自定义 Spinner 的外观

Fully customizing the look the Spinner

我想自定义折叠的微调器外观和下拉菜单的外观。在代码方面,这就是我正在做的:

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.country_arrays,
R.layout.ssi);
                //android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(R.layout.ssi2);
        final Spinner states = (Spinner) findViewById(R.id.spinner);
        states.setAdapter(adapter);

现在,问题是,ssissi2 布局都必须有 TextView 作为其 root,即:

ssi.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"

    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" 
    android:textColor="#00ff00"
    android:background="#000020"    
    />

ssi2.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"

    android:singleLine="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee" 
    android:textSize="10pt"
    android:textColor="#0000ff"
    android:background="#800000"/>

这有效地防止了对微调项进行 任何 种 customization/layout 操作。我想要的是能够为微调器的折叠外观嵌入自定义的 LinearLayout 之类的东西,以及为展开的下拉列表嵌入一个截然不同的布局——比如说 FrameLayout 带有图像和文本。即:

 <LinearLayout>
     <ImageView>...
     <TextView>....
     <LinearLayout> .... </LinearLayout>
 </LinearLayout>

EDIT1:有了 Aleksey 的回答,我就差不多了……除了,我该如何删除那个内置指示器(见图)?

Default style for collapsed spinner

EDIT2:我最终将微调器的 visibility 设置为 gone,然后有一个自定义小部件(比如 ImageView)将 onClick 事件转发到 Spinner。丑陋,但有效。

ImageView btnPlus = (ImageView)findViewById(R.id.btn_plus);
    btnPlus.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            spinner.performClick();
        }
    });

使用您自己的 ArrayAdapter。覆盖 getDropDownViewgetCustomDropDownView,如:

class CustomAdapter extends ArrayAdapter<String> {
    public CustomAdapter(Context context, int itemLayoutResourceId, arrayList) {
        super(context, itemLayoutResourceId, arrayList);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getCustomDropDownView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    private View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.spinner_item_dropdown, parent, false);
        }

        TextView label = (TextView) convertView.findViewById(R.id.textView);
        label.setText(arrayList[position]);

        return convertView;
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.spinner_item, parent, false);
        }

        TextView label = (TextView) convertView.findViewById(R.id.text2);
        label.setText(arraylist[position]);

        return convertView;
    }
}

并设置您自己的项目布局。就这些了。