4

我创建了一个像这样的Android“Spinner”元素:

<Spinner
    android:id="@+id/choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_span="2"
    android:background="@drawable/cell_shape"
    android:gravity="center"
    android:padding="3dp"
    android:text="Choice"
    android:textColor="@android:color/holo_orange_dark"  />

cell_shape.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#99CC3300"/>
    <corners android:radius="10px"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" /> 
</shape>

我创建了一个“custom_spinner.xml”文件来显示带有圆角的“Spinner-Elements”:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cell_shape"
    android:textColor="#988767" >
</TextView>

要显示“Spinner-Element”,我使用以下代码:

Spinner spinner = (Spinner) table.findViewById(R.id.choice);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, R.layout.custom_spinner);
adapter.setDropDownViewResource(R.layout.custom_spinner);
spinner.setAdapter(adapter);

现在的问题是微调器的元素项也有方角。似乎只有“TextView”有圆角,但微调器元素本身没有。

这里有什么问题?我只是使用了错误的元素类型吗?

问候

4

1 回答 1

3
Spinner spinner = (Spinner) table.findViewById(R.id.choice);
spinner.setAdapter(new MyCustomSpinnerAdapter(SearchScreen.this, R.layout.custom_dropDown_spinner, List));

布局:custom_dropDown_spinner

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:gravity="center"
    android:id="@+id/title" >

    <TextView
        android:id="@+id/custom_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/cell_shape"
        android:textColor="#988767" />

</LinearLayout>

Java代码:-

private class MyCustomSpinnerAdapter extends ArrayAdapter<ArrayList> {
        private ArrayList<String> objectsList = new ArrayList<String>();

        public MyCustomSpinnerAdapter(Context context, int textViewResourceId,
                ArrayList objects) {
            super(context, textViewResourceId, objects);
            this.objectsList = objects;
            // System.out.println("mak" + objectsList.size());
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
              //view show after click spinner
            return getCustomView(position, convertView, parent);
        }

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

        public View getCustomView(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.custom_dropDown_spinner, parent,
                    false);
            LinearLayout rowTitle = (LinearLayout) rowView
                    .findViewById(R.id.title);
                          rowTitle.setBackgroundResource(R.drawable.cell_shape);

            TextView textView = (TextView) rowView.findViewById(R.id.custom_spinner);

            textView.setText(objectsList.get(position).toString().trim());
            return rowView;
        }

        public View getCustomView1(int position, View convertView,
                ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View rowView = inflater.inflate(R.layout.custom_dropDown_spinner, parent,
                    false);
            LinearLayout rowTitle = (LinearLayout) rowView
                    .findViewById(R.id.title);



            TextView textView = (TextView) rowView.findViewById(R.id.custom_spinner);
            textView.setText(objectsList.get(position).toString().trim());
            textView.setTypeface(typeFace);
            // System.out.println("position " + position);
            return rowView;
        }
    }
于 2012-09-10T10:55:04.093 回答