Android:为Spinner项设置圆角

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);

问题在于Spinner的元素也具有方形边角。似乎只有“TextView”具有圆角,而不是Spinner元素本身。
这里出了什么问题?我使用了错误的元素类型吗?
谢谢。

尝试使用 android:background="#0000"<Spinner> 的背景设置为透明。 - Sherif elKhatib
@SherifelKhatib 嗯。没有起作用 :(。 - sk2212
也许你应该添加android:popupBackground="#0000"。 - Sherif elKhatib
1个回答

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

布局:自定义下拉选择器

<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;
        }
    }

在getCustomView中,你膨胀了custom_dropDown_spinner,但是custom_dropDown_spinner没有R.id.row。这不应该是R.id.title吗?在getCustomView1中,你膨胀了spinner_dropdown,但是你的示例中从未使用过它,也从未使用过膨胀的rowTitle。 - corey
对不起和谢谢 Corey。我在更新代码时犯了一个错误。我认为编辑过的代码会对你有帮助。 - mukesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接