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

时间:2014-09-18 11:19:02

标签: android colors android-spinner

enter image description here

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

enter image description here

7 个答案:

答案 0 :(得分:79)

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

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

其他解决方案(感谢Simon),如果您不想更改所有Spinners:

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);
}

答案 1 :(得分:33)

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

android:backgroundTint="@color/my_color"

答案 2 :(得分:17)

要获得正确的图片,您可以转到Android Asset Studio网站并选择Android Holo Colors Generator。这将创建您可能需要的所有资产,包括"三角形"。它还会生成实现更改的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>

正如您所看到的,这些样式也引用了许多drawable。

这是特定于&#34;三角形&#34;你想改变:

<?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在其评论中引用的那篇文章:

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

答案 3 :(得分: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>

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

答案 4 :(得分:4)

如果你有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 :(得分:4)

我在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"/>

Blue triangle

答案 6 :(得分:2)

以下是以编程方式解决该问题的其他方法。 (经过API 19及以上测试)。

请使用ViewCompat

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

spinner.setSupportBackgroundTintList的使用会引发错误