Change holo spinner text colour

2019-04-23 11:14发布

问题:

I have a spinner in a Holo theme dialog and am trying to change the text colour because it is very hard to read:

I have looked at android styles.xml, as well as many other answers, and believe that I am setting the custom style correctly; but it's just not getting picked up.

This is an extract from the dialog layout file where the spinner lives:

<Spinner
        android:id="@+id/spn_Type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:entries="@array/dose_type_options"
        style="@style/DialogSpinner" />

And these are the relevant entries in styles.xml in the values-v14 folder:

<style name="DialogSpinner" parent="@android:style/Widget.Holo.Spinner">
        <item name="android:spinnerItemStyle">@style/MySpinnerItem</item>
</style>

<style name="MySpinnerItem" parent="android:Widget.Holo.TextView.SpinnerItem">
    <item name="android:textAppearance">@style/MyTextAppearanceSpinnerItem</item>
</style>

<style name="MyTextAppearanceSpinnerItem" parent="android:TextAppearance.Holo.Widget.TextView.SpinnerItem">
    <item name="android:textColor">#FFF</item>
</style>

The dialog itself is forced to the Holo dark theme by using:

<style name="FibroDialog" parent="@android:style/Theme.Holo.Dialog">

</style>

Can anyone identify why the spinner text isn't white?

I have looked at other solutions, which suggest changing the colour in code, but this app supports 2.3.* upwards, so for those non-holo versions black text is fine, hence trying to do it by styles.

Thanks

Updated using answer from Woda below

The text colour of the initial value of the spinner is now white, which goes a long way to highlighting that there is a spinner there for the user:

But the text colour of the selectable items is still black. I guess it's not a massive deal, at least the existence of the spinner has been affirmed by getting the initial text changed to white. But I would be interested to know why the items are still black, and how to change them to white.

回答1:

Have you tried to accept the SpinnerItemStyle to your Theme? So all Spinners in your App would've the same style. I'm using it like this and it works:

theme.xml:

<style name="exampleTheme" parent="android:Theme.Holo.Light">
    <item name="android:spinnerItemStyle">@style/SpinnerItem_example</item>
    ...
</style>

style.xml:

<style name="SpinnerItem_example" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#000000</item>
</style>

Update: Taking a deeper look into the styles.xml brought me this:

<style name="Widget.DropDownItem.Spinner">
        <item name="android:checkMark">?android:attr/listChoiceIndicatorSingle</item>
</style>

<style name="Widget.DropDownItem">
        <item name="android:textAppearance">@style/TextAppearance.Widget.DropDownItem</item>
        <item name="android:paddingStart">@dimen/dropdownitem_text_padding_left</item>
        <item name="android:paddingEnd">@dimen/dropdownitem_text_padding_right</item>
        <item name="android:gravity">center_vertical</item>
</style>

So you probably need to customize the Widget.DropDownItem and accept it in your theme.

...
<item name="dropDownItemStyle">@android:style/Widget.DropDownItem</item>
...

For customizing my application the following two links helped me a lot to understand the structure of the different views. These two files are part of the android source code. May be it helps you too.

  • themes.xml
  • styles.xml


回答2:

I fixed it by calling

mArrayAdapter.setDropDownViewTheme(mActivity.getTheme());

Hope this helps someone ;)



回答3:

You can access the internal TextView in code without changing any styles. This is how I handled enabling and disabling Spinners

The .getSelectedView() did not work for me. So I tricked the Spinner to "show" being disabled.

You will need to define your own colors for the "disabled" look.

For Example:

R.color.blue_text //means enabled
R.color.gray_text //means disabled

So to disable my spinner:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.gray_text));
mySpinner.setEnabled(false);
mySpinner.setFocusable(false);

To enable my spinner:

((TextView)mySpinner.getChildAt(0)).setTextColor(getResources().getColor(R.color.blue_text));
mySpinner.setEnabled(true);
mySpinner.setFocusable(true);

The getChildAt(0) function allows you to access the first item in the spinner, which is what you show on the screen as a TextView.

You don't need to change styles or modify any XML. Just do this in your code, even within event methods, you should be fine.