【问题标题】:Is there any easy way to change Spinner dropdown color in Android?有没有什么简单的方法可以在 Android 中更改 Spinner 下拉菜单的颜色?
【发布时间】:2017-10-17 07:55:10
【问题描述】:

我创建了我的主题以与应用程序一起使用,主题的父主题是 Theme.AppCompat.Light.NoActionBar

顺便说一句,我想要白色背景和黑色文字。

这是适配器代码

     val adapter = ArrayAdapter.createFromResource(activity,
                R.array.email_type_array, android.R.layout.simple_spinner_item)

     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
     child.spinner.adapter = adapter

有没有什么简单的方法可以在 Android 中更改 Spinner 下拉菜单的颜色?

【问题讨论】:

    标签: android android-layout android-spinner android-theme


    【解决方案1】:

    是的。您可以在您的 xml 中使用以下属性来微调器

    android:popupBackground="YOUR_HEX_COLOR_CODE"
    

    更改文本颜色等为您的微调器项目制作自定义 XML 文件。

    spin_item.xml:

    然后提供您想要的颜色和尺寸:

    <?xml version="1.0" encoding="utf-8"?>
    
    <TextView  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:textColor="#000000"         
        android:padding="4dp"
        />
    

    然后像这样使用它:

    val adapter = ArrayAdapter.createFromResource(activity,
                    R.array.email_type_array, android.R.layout.simple_spinner_item)
    adapter.setDropDownViewResource(R.layout.spin_item)
    

    【讨论】:

    • 是的,但文字仍然是白色的
    • 更新了答案。请检查。
    • 谢谢,实际上我已经找到了另一种方法,例如android:popupBackground="YOUR_HEX_COLOR_CODE" &lt;--- to set background android:popupTheme="@android:style/ThemeOverlay.Material.Dark" &lt;--- to set text color 但由于灵活性,我更喜欢使用您的解决方案。我可以自己设置文本的样式,不必遵循 Material 主题
    【解决方案2】:

    要更改 下拉背景颜色,请在您的 Spinner 小部件的 xml 文件中使用 android:popupBackground="@color/aColor"

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:popupBackground="@color/aColor" />
    

    styles.xmlfile 上使用浅色主题时,微调器下拉图标颜色 将为黑色,但请注意,如果您使用&lt;item name="android:textColorSecondary"&gt;@color/aColor&lt;/item&gt;,下拉图标将选择该颜色:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorSecondary">@color/aColor</item>
    

    甚至你的问题是要改变 下拉背景颜色 我来到这里是因为我试图理解为什么我的 微调器下拉图标颜色 是不同的颜色,直到我发现 (android:textColorSecondary) - 所以希望这对其他人也有帮助。

    【讨论】:

      【解决方案3】:

      通过代码

      Spinner spinner = (Spinner) findViewById(R.id.spinner);
      spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);
      

      或通过 XML

      对于 API 21+:

      <Spinner
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:backgroundTint="@color/red" />
      

      或者如果你使用支持库,你可以使用:

      <android.support.v7.widget.AppCompatSpinner
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:backgroundTint="@color/red" />
      

      Please check the answer here

      【讨论】:

      • 不,它改变的是微调器本身的背景,而不是下拉的背景颜色。
      【解决方案4】:

      在您的代码中,在 onCreate() 中添加以下内容:

       Spinner spinner = (Spinner) findViewById(R.id.spinner);
       spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), 
       PorterDuff.Mode.SRC_ATOP);
      

      【讨论】:

      • 不,它改变的是微调器本身的背景,而不是下拉背景颜色。
      【解决方案5】:

      创建一个如下所示的新布局文件

      <?xml version="1.0" encoding="utf-8"?>
      <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@android:id/text1"
          style="?android:attr/spinnerDropDownItemStyle"
          android:singleLine="true"
          android:layout_width="match_parent"
          android:layout_height="?android:attr/dropdownListPreferredItemHeight"
          android:ellipsize="marquee"
          android:background="MY REQUIRED COLOR"/>
      

      在我说MY REQUIRED COLOR的地方请把它设置成你想要的颜色。

      还要确保不要更改android:id 属性,因为arrayadapter 将使用它来将文本设置为textview

      然后像这样在创建过程中将其设置为您的arrayadapter

      val adapter = ArrayAdapter.createFromResource(activity,
                      R.array.email_type_array, .R.layout.custom_ simple_spinner_item)
      

      【讨论】:

        【解决方案6】:

        location_Survey_Spin = findViewById(R.id.location_Survey_Spinner); location_Survey_Spin.getBackground().setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP); //它会改变微调器下拉菜单的颜色

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-14
          • 1970-01-01
          • 2021-09-27
          • 1970-01-01
          • 2020-01-10
          • 2017-12-18
          相关资源
          最近更新 更多