在Android中更改Spinner上小三角的颜色

60

enter image description here

我该如何更改下拉列表(Spinner)右下角小三角的颜色,如下图所示?目前默认为灰色。

enter image description here


2
根据要求设置自定义背景。 - Developer
只是一张图片中的小三角形?SDK 中必须有一个带灰色的图片。你能提供链接让我复制粘贴并仅更改颜色吗? - Yawar
2
希望能对您有所帮助... http://www.gersic.com/blog.php?id=57 - Developer
@Ricky,这是一篇很好的文章,非常清晰地解释了这个过程。我已经在我的答案底部包含了它,并注明了你的贡献。 - Richard Le Mesurier
9个回答

89

最好、最简单的解决方案:

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

如果您不想更改所有旋转选择器,可以使用其他解决方案(感谢Simon):

Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();

spinnerDrawable.setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    spinner.setBackground(spinnerDrawable);
}else{
    spinner.setBackgroundDrawable(spinnerDrawable);
}

不太好,这会改变所有旋转器的颜色。 - Simon
1
为了仅将其应用于特定的Spinner,您必须复制drawable: Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable(); spinnerDrawable.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP); if (Build.VERSION.SDK_INT >= 16) spinner.setBackground(spinnerDrawable);https://dev59.com/amsz5IYBdhLWcg3wVGEy#7980637 - Simon
2
对我来说,它并不是所有微调器都会改变颜色的真实情况。 - 4ndro1d
我也一样 @4ndro1d。也许这与你运行它的SDK级别有关。我正在测试6.0.1。 - Flyview

43

Lollipop 开始,您可以在 xml 上设置背景色调。

android:backgroundTint="@color/my_color"

请注意,仅当您未为您的Spinner设置背景时,此方法才有效。 - Muhammed Refaat

19

为了获取正确的图片,您可以访问Android Asset Studio网站,并选择Android Holo Colors生成器。该工具会生成您可能需要的所有资源,包括“三角形”图标,同时还会生成实现更改的XML文件。


这里是XML文件的示例:

自定义颜色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="apptheme_color">#33b5e5</color>
</resources>

自定义旋转器样式:

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="SpinnerAppTheme" parent="android:Widget.Spinner">
      <item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
      <item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
  </style>

  <style name="SpinnerDropDownItemAppTheme" parent="android:Widget.DropDownItem.Spinner">
      <item name="android:checkMark">@drawable/apptheme_btn_radio_holo_light</item>
  </style>
</resources>

自定义应用程序主题:

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="@style/_AppTheme"/>

  <style name="_AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:spinnerStyle">@style/SpinnerAppTheme</item>

    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItemAppTheme</item>

  </style>

</resources>

如您所见,这些样式还引用了许多可绘制对象。

这里是特定于您想要更改的“三角形”的可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false"
          android:drawable="@drawable/apptheme_spinner_disabled_holo_light" />
    <item android:state_pressed="true"
          android:drawable="@drawable/apptheme_spinner_pressed_holo_light" />
    <item android:state_pressed="false" android:state_focused="true"
          android:drawable="@drawable/apptheme_spinner_focused_holo_light" />
    <item android:drawable="@drawable/apptheme_spinner_default_holo_light" />
</selector>

我认为这不是列出每个文件的正确位置,因为您可以直接从引用工具中获取所有文件。


注意:这是一种为您的整个应用程序设置主题的方式,以便将所有旋转器更新为自定义样式。

如果您想要快速、简单地仅更改一个旋转器,请查看Ricky在评论中提到的那篇文章:

我建议您阅读该文章,因为它解释了这个过程非常好,并且将帮助您理解我答案中的其他部分。


1
Tom Gersic的文章链接已失效。我想我在这里找到了一份副本:http://www.synchronoussoft.net/blog/tom-gersic - muetzenflo
1
什么是 apptheme_spinner_background_holo_light - IgorGanapolsky

15

我在Android开发方面还非常新手,因此也许应该对我的建议持保留态度,但是这里的任何答案都似乎与我正在使用的下拉框实现方式无关。

我在寻找什么

android:backgroundTint="@color/colorPrimary"

这是完整的<Spinner>标签:

        <Spinner
            android:id="@+id/coin_selector"
            android:layout_width="wrap_content"
            android:layout_height="21dp"
            android:layout_margin="26dp"
            android:layout_weight="1"
            android:textColor="#FFFFFF"
            android:dropDownSelector="@color/colorAccent"
            android:backgroundTint="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            tools:layout_editor_absoluteY="89dp"/>

蓝色三角形


android:backgroundTint 需要 API 级别 21。 - Numair

7

这里还有另一种程序解决方法(已在API 19及以上版本上测试)。

可以使用ViewCompat来解决此问题。

ViewCompat.setBackgroundTintList(spinner, ColorStateList.valueOf(your_color));

spinner.setSupportBackgroundTintList 的使用会抛出错误。


1
很好 - 这对我来说使用常规的Spinner就可以了,不需要AppCompatSpinner。好答案。 - Vin Norman

5
如果您的minSdkVersion是21,那么非常简单:
<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/triangleColor" />

其中triangleColor是您的颜色。


5

只需按照这样做。这将帮助您解决问题。

<RelativeLayout 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="spinner background image">

        <Spinner       
        android:layout_width="match_parent"
        android:layout_height="match_parent"        
        android:background="@null"/>

        <ImageView 
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="arrow image" />

    </RelativeLayout>

或者

我刚才查看了我为另一个问题提供的答案: 自定义 Spinner


4

最低SDK版本为14的解决方案。您可以使用支持库中的AppCompatSpinner:

AppCompatSpinner spinner = (AppCompatSpinner) view.findViewById(R.id.my_spinner);
spinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(context, R.color.my_color));

0

我尝试过样式,但发现这行代码可以在 Kotlin 中显示 Spinner 的向下箭头:

ViewCompat.setBackgroundTintList(binding.ddnSpots, ColorStateList.valueOf(Color.parseColor("#4a3728")))

请注意,与传统的视图绑定相比,'ddnSpots' 绑定为片段的一个元素。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接