更改 XML 中 Spinner 下拉箭头的颜色

问题描述 投票:0回答:8

正如我在问题中所写,我想更改 XML 中

Spinner
的下拉箭头(默认箭头,不是自定义箭头或类似的东西)的颜色,但问题是我无法在XML
中找不到任何可以引用它的内容。

可能吗?如果是的话,我该如何改变颜色?

android android-layout colors android-spinner android-color
8个回答
180
投票
可以通过三种方法来实现这一目标。

1。通过代码:

在您的 xml 中,确保您的微调器有一个 id。假设我们有一个 id 为“spinner”的微调器。

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

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

其中红色是您在值文件夹中的

colors.xml中定义的颜色。

2。通过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" />

3.通过绘图:

您可以使用此在线工具:

http://android-holo-colors.com

这将为您想要的任何视图生成具有您喜欢的颜色的自定义绘图。确保选择微调器,然后下载资源。


15
投票
我很惊讶没有人指出这一点,但你可以直接子类化

Widget.AppCompat.Spinner

 并更改 
backgroundTint


<style name="Spinner" parent="Widget.AppCompat.Spinner"> <item name="backgroundTint">@color/spinnerTint</item> </style>

并将其应用到

Spinner



<Spinner style="@style/Spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:spinnerMode="dropdown" />
    

13
投票
使用backgroundTint属性

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

如果 min_SDK

AppCompatSpinner< 21(Lollipop) you should use

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

8
投票

如果API 21+){

您可以直接在 Spinner 中使用

android:backgroundTint="@color/color"

<Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="match_parent" android:backgroundTint="@color/color" />
}

否则{

创建自己的风格:

<style name="spinner_style" parent="Widget.AppCompat.Spinner"> <item name="backgroundTint">@color/color</item> </style>
然后进入 Spinner:

<Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="match_parent" style="@style/spinner_style"/>
}

注意:您可以在所有 API 中使用 style 方法。


4
投票
<Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="#00000" />

仅适用于 API 级别 21 以上


3
投票
试试这个:

spinner_age.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.spinner_icon), PorterDuff.Mode.SRC_ATOP);
    


0
投票

第 1 步。 使用以下方法设计您自己的微调器背景:

https://icons8.com/icons/set/drop-down

第 2 步。 使用 Paint 在向下箭头旁边添加一个白色框并另存为 .PNG 文件。

它应该看起来像这样:

第 3 步。 将 .PNG 图像复制并粘贴到应用程序的 drawable 文件夹中

步骤 4.activity_main.xml,复制并粘贴以下代码:

<TextView android:id="@+id/spinnersearchtext" android:layout_width="80dp" android:layout_height="40dp" android:layout_marginEnd="5dp" android:gravity="center|end" android:text="Search:" android:textColor="#FFFFFF" android:textSize="20dp" android:textStyle="bold" android:visibility="visible" app:layout_constraintEnd_toStartOf="@+id/spinner1" app:layout_constraintHorizontal_bias="1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Spinner android:id="@+id/spinner1" android:layout_width="100dp" android:layout_height="40dp" android:layout_marginLeft="50dp" android:background="@drawable/dropdownarrow" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

最终结果:

© www.soinside.com 2019 - 2024. All rights reserved.