Android - 以编程方式更改微调器弹出对话框颜色

时间:2018-05-10 14:44:24

标签: android kotlin dialog spinner

我有一个Spinner对象,如下所示:

<Spinner
    android:id="@+id/createsub_category_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:spinnerMode="dialog" />

和spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:gravity="end"
    android:padding="5dip"
    android:textAlignment="viewEnd"
    android:textColor="#FFFFFF"
    android:textSize="18sp" />

然后在代码中:

categorySpinner = find(R.id.createsub_category_spinner)
        val categoryAdapter = ArrayAdapter.createFromResource(this,
                R.array.categories, R.layout.spinner_item)
        categorySpinner.adapter = categoryAdapter
        categorySpinner.setPopupBackgroundResource(sub.color)
        categorySpinner.setSelection(sub.category)

我希望setPopupBackgroundResource(sub.color)修改对话框的默认白色背景。但这不会发生?我错过了什么?

经过一些进一步的测试后,我注意到如果我有android:spinnerMode="dropdown",上面显示的代码可以工作,当选择对话框模式时,某些方法同样不适用。

2 个答案:

答案 0 :(得分:1)

根据documentation

Set the background drawable for the spinner's popup window of choices. Only valid in MODE_DROPDOWN; this method is a no-op in other modes.

所以不幸的是,这不会在对话模式下工作。

答案 1 :(得分:0)

试试这个..

spinner.setPopupBackgroundResource(R.drawable.spinner_background);

这里是spinner_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
相关问题