Zmiana koloru małego trójkąta na spinner w Androidzie

Tutaj wpisz opis obrazka

Jak mogę zmienić kolor małego trójkąta w prawym dolnym rogu spinnera, jak pokazano na obrazku? Teraz jest wyświetlany domyślny szary kolor. like this

Tutaj wpisz opis obrazka

Author: Yawar, 2014-09-18

8 answers

Aby uzyskać prawidłowe obrazy, możesz przejść do strony Android Asset Studio i wybrać Android Holo Colors Generator. Spowoduje to utworzenie wszystkich potrzebnych zasobów, w tym "trójkąta". Generuje również pliki XML, które implementują zmiany.


Oto przykładowe pliki XML:

Własny Kolor:

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

Własny Styl Spinnera:

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

Jak widać, istnieje również wiele drawables, do których odnoszą się te style.

Oto jeden specyficzny dla "trójkąta", który chcesz zmienić:

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

Myślę, że nie jest to właściwe miejsce na listę wszystkich plików, ponieważ możesz pobrać je wszystkie bezpośrednio z narzędzia, do którego się odwołujesz.


Uwaga: jest to sposób na motyw całej aplikacji tak, że wszystkie Spinnery są aktualizowane do niestandardowego stylu.

Jeśli szukasz szybkiego i brudnego sposobu na zmianę tylko jednego spinnera, sprawdź z artykułu, o którym wspomina Ricky w komentarzu:

I tak polecam ją przeczytać, ponieważ bardzo dobrze wyjaśnia proces i pomoże Ci zrozumieć resztę mojej odpowiedzi.

 15
Author: Richard Le Mesurier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-04 10:46:33

Najlepsze i najłatwiejsze rozwiązanie:

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

Inne rozwiązanie (dzięki Simonowi) jeśli nie chcesz zmienić wszystkich spinnerów:

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);
}
 77
Author: toni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-21 19:09:08

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

android:backgroundTint="@color/my_color"
 27
Author: Luis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-11 20:23:03

Po prostu zrób tak. Pomoże to rozwiązać twój 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>

Lub

Właśnie spojrzałem na odpowiedź, którą podałem dla innego zapytania: Custom Spinner

 4
Author: RIJO RV,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:26:20

Rozwiązanie dla minimum SDK w wersji 14. Możesz użyć AppCompatSpinner z biblioteki wsparcia:

AppCompatSpinner spinner = (AppCompatSpinner) view.findViewById(R.id.my_spinner);
spinner.setSupportBackgroundTintList(ContextCompat.getColorStateList(context, R.color.my_color));
 2
Author: vmayatskii,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-12 08:51:03

Wciąż jestem nowy w rozwoju Androida, więc może przyjmij moją radę z przymrużeniem oka, ale żadna z odpowiedzi tutaj nie odnosiła się do implementacji spinnera, z której korzystałem.

Czego szukałem

android:backgroundTint="@color/colorPrimary"

Oto cały <Spinner> tag:

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

Niebieski trójkąt

 2
Author: jessems,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-12 15:12:26

Jeśli masz minSdkVersion 21, jest to bardzo proste:

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

Gdzie triangleColor jest Twoim kolorem.

 1
Author: Vladyslav Ulianytskyi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-10 15:00:26

Tutaj jest inny sposób, aby rozwiązać ten programowo, jak również. (Testowane na API 19 i powyżej).

Użyj ViewCompat do tego.

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

Użycie spinner.setSupportBackgroundTintList rzuca błąd

 0
Author: sud007,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-25 08:43:49