【问题标题】:Change spinner dropdown item text color and text size更改微调器下拉项文本颜色和文本大小
【发布时间】:2019-06-21 20:18:29
【问题描述】:

我正在使用 android 数据绑定,作为其中的一部分,我有一个带有项目的微调器(在 strings.xml 中声明的数组)作为

android:entries="@string/items"

现在我想更改下拉项的文本颜色和大小。 我想在不为项目创建单独布局并通过 java 使用它的情况下执行此操作(将项目布局传递给数组适配器并将该适配器设置为微调器)。

我尝试了很多方法并到处搜索,但没有找到合适的解决方案。

感谢任何帮助。

【问题讨论】:

    标签: android android-spinner android-databinding


    【解决方案1】:

    你应该使用android.support.v7.widget.AppCompatSpinner

     <android.support.v7.widget.AppCompatSpinner
                        style="@style/Widget.AppCompat.Spinner.Underlined"
                        android:theme="@style/Spinner"
                        android:entries="@array/special_fx_arrays"
                        android:textSize="@dimen/text_size_normal"/>
    

    这里要输入styles.xml的样式

    <style name="Spinner" parent="Widget.AppCompat.Light.DropDownItem.Spinner">
            <item name="android:paddingStart">0dp</item>
            <item name="android:paddingEnd">0dp</item>
            <item name="android:textColor">@color/white</item>
            <item name="android:backgroundTint">@color/red</item>
            <item name="android:textSize">14sp</item>
    </style>
    

    【讨论】:

    • 惊人的答案,特别是因为您不需要以编程方式实现任何东西。不过需要注意的一点是,这仅在使用android:theme 时有效,因为使用style 并不适用于所有属性,反之亦然。您始终可以同时使用两者(themestyle)以确保正确覆盖所有属性。希望对您有所帮助!
    【解决方案2】:

    可以通过xml创建样式并设置为spinner

     <style name="customStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
            <item name="android:textColor">#000</item>
            <item name="android:textSize">12dp</item>
     </style>
    

    customStyle 设置为您的微调器

    【讨论】:

      【解决方案3】:

      只需创建一个带有 textview 及其属性的新 xml: my_spinner.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="12sp"
         android:textColor="#f51111"
      />
      

      Luego set el nuevo xml en el ArrayAdapter:

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_spinner,data);
          spinner.setAdapter(adapter);
      

      【讨论】:

        猜你喜欢
        • 2012-03-17
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多