微调器自定义布局保持默认布局

时间:2013-09-20 13:19:59

标签: android android-layout android-spinner

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

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" />

顺便说一句,下拉列表运行得很好。

我之前已经在同一个应用程序中完全按照相同的方式执行了此操作,并且它的工作方式非常出色,但不在此处。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

以下是示例代码,您可以参考此内容,了解自定义布局如何膨胀以及您做错了什么。希望它有所帮助:

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>

答案 1 :(得分:0)

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

selector.xml这个xml可以用作微调器背景

<?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" />