0

嗨,我尝试在微调器上设置自定义布局。我的布局已添加,但默认微调器布局仍在我的自定义布局周围,如下所示:

http://i.imgur.com/YjGZo7x.png?1

我像这样在我的活动的 onCreate 方法中初始化我的微调器:

    Spinner s = (Spinner)findViewById(R.id.s_civilty);
    ArrayAdapter <String> adapter =
            new ArrayAdapter<String> (this, R.layout.spinner, new String[] {"Mademoiselle","Madame","Monsieur"});
    adapter.setDropDownViewResource(R.layout.spinner_dropdown);
    s.setAdapter(adapter);

还有spinner.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue1"
    android:drawableRight="@drawable/ic_arrow_down_simple"
    android:gravity="center"
    android:paddingLeft="5dp"
    android:textColor="@drawable/btn_default"
    android:textSize="25sp" />

顺便说一句,下拉菜单工作得很好。

我之前已经在同一个应用程序中以完全相同的方式做到了这一点,并且它的工作方式异常,但不是在这里。

在此先感谢您的帮助。

4

2 回答 2

1

这是示例代码,您可以参考this并查看自定义布局如何膨胀以及您做错了什么。希望能帮助到你:

public class Style extends Activity{
    String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
            "china", "indiaA", "indiaC" };
    String[] images = { "R.drawable.ic_launcher", "R.drawable.ic_launcher",
            "R.drawable.ic_launcher", "R.drawable.ic_launcher",
            "R.drawable.ic_launcher", "R.drawable.ic_launcher",
            "R.drawable.ic_launcher","R.drawable.ic_launcher" };
    Spinner spin;
    TextView txt1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.npicker);
        initcomponents();
        actionlistener();
        CustomAdapter spinneradapter = new CustomAdapter(this,
                android.R.layout.simple_spinner_item, items,images);
        spinneradapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(spinneradapter);

    }

    private void actionlistener() {

    }

    private void initcomponents() {
        spin = (Spinner) findViewById(R.id.npicker_spnr);

    }

    public class CustomAdapter extends ArrayAdapter<String> {
        String[] items,images;
        Context context;
        int rid;
        TextView textview;
        ImageView imageview;
        LayoutInflater inflater;

        public CustomAdapter(Context context, int rid, String[] items,String[] images) {
            super(context, rid, items);
            this.items = items;
            this.context = context;
            this.rid = rid;
            this.images=images;

            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        public int getCount() {

            return items.length;
        }

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

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

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

            if (convertview == null) {
                convertview = inflater.inflate(R.layout.spinneritems, parent, false);
                textview = (TextView) convertview
                        .findViewById(R.id.spinneritem_txt);
                imageview = (ImageView) convertview
                        .findViewById(R.id.spinneritem_iv);
                imageview.setBackgroundResource(R.drawable.ic_launcher);
                textview.setText(items[position]);

            }

            return convertview;
        }

    }


}

这是微调器项 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/spinneritem_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/spinneritem_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="Hello i am android logo" />

</LinearLayout>
于 2013-09-20T13:28:29.237 回答
0

请检查以下我用来更改微调器背景的代码可能对您有所帮助

selector.xml 这个xml可以作为spinner背景

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/text_filed_login"/>  
</selector>

下面是微调器的片段

<Spinner
                android:id="@+id/spinner1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/addSpinner"
                android:layout_marginLeft="5dip"
                android:background="@drawable/groupselector"
                android:paddingBottom="2dp"
                android:paddingLeft="10dp"
                android:paddingTop="2dp"
                android:text="Select a group"
                android:textColor="#a5a5a5"
                android:textSize="13dp"
                android:typeface="sans" />
于 2013-09-20T13:38:58.457 回答