【问题标题】:Android - Change Spinner Popup Dialog Color ProgramaticallyAndroid - 以编程方式更改微调器弹出对话框颜色
【发布时间】:2018-05-11 08:13:35
【问题描述】:

我有一个如下所示的 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",上面显示的代码可以工作,但在选择对话模式时,同样的代码不适用。

【问题讨论】:

    标签: android kotlin dialog spinner


    【解决方案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.

    不幸的是,这在对话模式下不起作用。

    【讨论】:

      【解决方案2】:

      试试这个..

      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>
      

      【讨论】:

      • 谢谢,但是这样我不能动态设置颜色,我需要一种方法来以编程方式执行此操作,以便颜色根据sub.color 值变化
      • 您以编程方式获得了多少种颜色?您可以为每种颜色创建一个可绘制背景...
      • 这根本不是最佳选择,我们正在讨论,而且数量将增加,大约 40 种颜色。
      • 你试过 spinner.setBackgroundColor() 吗??
      • 是的,它改变了微调器的背景颜色,而不是弹出对话框的背景颜色
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2011-11-08
      • 1970-01-01
      • 2015-04-02
      • 1970-01-01
      相关资源
      最近更新 更多