문제

We are trying to change the color of the text within a spinner.

This is the XML in our activity layout:

        <Spinner
            android:id="@+id/SpinnerFeedbackType"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:entries="@array/feedbacktypelist"
            android:textColor="@color/white" >
        </Spinner>

This is the strings.xml

<color name="white">#FFFFFF</color>

<!-- Other string resources also dfined in this file… -->
<string name="feedbacktype1">www.currycottageferndown.co.uk</string>
<string name="feedbacktype2">192.168.1.1</string>
<string name="feedbacktype3">8.8.8.8</string>
<string name="feedbacktype4">www.bournemouth.ac.uk</string>

The color remains black.

We don't have an array adapter for some reason, this is our spinner code.

Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
            final String feedbackType = feedbackSpinner.getSelectedItem().toString();

Any ideas?

도움이 되었습니까?

해결책

Can try below code:
Add below code in styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MooTheme" parent="android:Theme">
        <item name="android:spinnerItemStyle">@style/MooSpinnerItem</item>
    </style>

    <style name="MooSpinnerItem" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/MooTextAppearanceSpinnerItem</item>
    </style>

    <style name="MooTextAppearanceSpinnerItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
        <item name="android:textColor">#F00</item>
    </style>
</resources>

Then add this to the application tag in your AndroidManifest.xml

android:theme="@style/MooTheme"

다른 팁

You need to create a custom layout in order to create a custom Spinner.

Create a layout file spinner_item.xml

Create your own layout file with a custom definition for the spinner item. This is my spinner_item.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textColor="#ff0000" />

Change your declaration of the spinner to use the R.layout.spinner_item:

// Declare the spinner
Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
// Set the custom layout to the array adapter, and send your array feedbacktypelist with the data
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.feedbacktypelist, R.layout.spinner_item);
feedbackSpinner.setAdapter(adapter);

This is the result

enter image description here

Now, to customize the dropdown list items you will need to create a new layout file to the dropdown.

Create a layout file called spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#aa66cc"/>

Change the declaration of the spinner

// Declare the spinner
Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
// Set the custom layout to the array adapter, and send your array feedbacktypelist with the data
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.feedbacktypelist, R.layout.spinner_item);
// Set the custom layout for the drop down items.
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
feedbackSpinner.setAdapter(adapter);

This is the result:

enter image description here

Basically that's it.

Source of the answer. How to change a Spinner text size, color or overall style

Hope this helps you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top