更改android中微调器上小三角形的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25911287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:49:21  来源:igfitidea点击:

Change colour of small triangle on spinner in android

androidcolorsandroid-spinner

提问by Yawar

enter image description here

在此处输入图片说明

How can I change the colour of small triangle at the bottom right corner of spinner like shown in the image? It is showing default grey colour right now. like this

如何更改微调器右下角小三角形的颜色,如图所示?它现在显示默认的灰色。像这样

enter image description here

在此处输入图片说明

采纳答案by Richard Le Mesurier

To get the correct images, you can go to Android Asset Studiosite and choose the Android Holo Colors Generator. This will create all the assets you might need, including the "triangle". It also generates the XML files that implement the changes.

要获得正确的图像,您可以转到Android Asset Studio站点并选择Android Holo Colors Generator。这将创建您可能需要的所有资产,包括“三角形”。它还生成实现更改的 XML 文件。



Here are the example XML files:

以下是示例 XML 文件:

Custom Color:

自定义颜色:

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

Custom Spinner Style:

自定义微调样式:

<?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>

Custom Application Theme:

自定义应用主题:

<?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>


As you can see, there are also many drawables that are referenced by these styles.

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

Here is the one specific to the "triangle" you want to change:

这是特定于您要更改的“三角形”的一个:

<?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>

I don't think this is the right place to list every file completely, since you can get them all directly from the referenced tool.

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



Note:this is the way to theme your whole app so that all spinners are updated to the custom style.

注意:这是为整个应用程序设置主题的方式,以便所有微调器都更新为自定义样式。

If you are looking for a quick and dirty way to change just one spinner, then check out that article referenced by Ricky in his comment:

如果您正在寻找一种快速而肮脏的方法来更改一个微调器,请查看 Ricky 在他的评论中引用的那篇文章:

I recommend reading it anyway, because it explains the process very well, and will help you to understand the rest of my answer.

无论如何,我建议阅读它,因为它很好地解释了这个过程,并且会帮助您理解我的其余答案。

回答by toni

The best and easiest solution:

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

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

Other solution (Thanks to Simon) if you don't want change all Spinners:

如果您不想更改所有 Spinner,其他解决方案(感谢 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);
}

回答by Luis

From Lollipopon, you can set the background tint on xml,

棒棒糖开始,您可以在xml上设置背景色调,

android:backgroundTint="@color/my_color"

回答by jessems

I'm still very new at Android development, so perhaps take my advice with a grain of salt, but none of the answers here seemed to relate to the spinner implementation I was using.

我对 Android 开发还是很陌生,所以也许我对我的建议持保留态度,但这里的答案似乎都与我使用的微调器实现无关。

What I was looking for

我在寻找什么

android:backgroundTint="@color/colorPrimary"

Here's the entire <Spinner>tag:

这是整个<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"/>

Blue triangle

蓝色三角形

回答by sud007

Here is other way to resolve that programatically as well. (Tested on API 19 & Above).

这是以编程方式解决该问题的其他方法。(在 API 19 及更高版本上测试)。

Use ViewCompatfor this.

ViewCompat为此使用。

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

Usage of spinner.setSupportBackgroundTintListthrows an error

使用spinner.setSupportBackgroundTintList抛出错误

回答by RIJO RV

Just do as like this. This will helps you to solve your problem.

就这样做吧。这将帮助您解决您的问题。

<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>

or

或者

Just had a look on the answer which I given for another query: Custom Spinner

刚刚查看了我为另一个查询给出的答案: Custom Spinner

回答by vmayatskii

The solution for minimum SDK version 14. You can use AppCompatSpinner from Support Library:

SDK 最低版本 14 的解决方案。您可以使用 Support Library 中的 AppCompatSpinner:

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

回答by Vladyslav Ulianytskyi

If you have minSdkVersion 21, it is very simple:

如果您有 minSdkVersion 21,则非常简单:

<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/triangleColor" />

where triangleColor is your color.

其中triangleColor 是您的颜色。