Android: Where is the Spinner widget's text color attribute hiding?
I'm trying to change the text color of the single item that is displayed in the spinner button after you select an item from the dropdown. I've been perusing the themes.xml and styles.xml in the Android SDK for an hour now, and I can't seem to find where the Spinner is getting the color value from.
To clarify, I'm NOT trying to 开发者_开发百科change the color of a dropdown item, I'm trying to change the color of the spinner's displayed text when there is no dropdown. I guess you could call it the spinner's 'button' text.
I think it's probably this bit in styles.xml
<style name="Widget.TextView.SpinnerItem">
<item name="android:textAppearance">@style/TextAppearance.Widget.TextView.SpinnerItem</item>
</style>
<style name="Widget.DropDownItem.Spinner">
<item name="android:checkMark">?android:attr/listChoiceIndicatorSingle</item>
</style>
-= EDIT =- Here's the result:
and here's how it's done:
<?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 just add this to the application tag in your AndroidManifest.xml
android:theme="@style/MooTheme"
Yeah CaseyB is correct.
Here's how I set the spinner text color, a little simple example:
styles.xml
<style name="Theme.NoTitleBar.WithColoredSpinners" parent="@android:style/Theme.NoTitleBar">
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
</style>
<style name="SpinnerItem" parent="@android:style/Widget.TextView.SpinnerItem">
<item name="android:textColor">#00FF00</item>
</style>
<style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.DropDownItem.Spinner">
<item name="android:textColor">#FF0000</item>
</style>
</resources>
Then in your manifest:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.NoTitleBar.WithColoredSpinners" >
The text on the outside of all your spinners will now be Green and the text on the dropdowns will be red.
I did this using another simple technique,
copy the simple_spinner_item.xml and simple_spinner_dropdown_item.xml from Android res/layout folder and copy them in your project.
Then modify the following lines
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, Android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(Android.R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);
as:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinnerSubject.setAdapter(adapter);
The rest is easy, you can now edit the simple_spinner_item.xml to edit the appearence of one visible item on the spinner widget, and edit the simple_spinner_dropdown_item.xml to change the appearance of drop down list.
For example, my activity layout contains:
<Spinner
android:id="@+id/mo_spinnerSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/spinnerset_background" />
and my simple_spinner_item.xml now contains:
<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:singleLine="true"
android:textColor="@color/custom_white"
android:textSize="16sp" />
and the simple_spinner_dropdown_item.xml looks like:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:background="@color/custom_black"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="@color/custom_white" />
You can use setOnItemSelectedListener on Spinner object,
spinnerObject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
((TextView)parentView.getChildAt(0)).setTextColor(Color.rgb(249, 249, 249));
// OR ((TextView)parentView.getChildAt(0)).setTextColor(Color.RED);
}
});
its very simple actually. I was looking for all over you just need to create style and set on spinner
first create your theme in Style.xml
<style name="spinnerTheme" parent="android:Theme">
<item name="android:textColor">@color/gray_dark</item>
</style>
then in your xml where you've set your spinner add this :
android:theme="@style/spinnerTheme"
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="10dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:layout_span="3"
android:layout_weight="1.3"
android:theme="@style/spinnerTheme"
android:textSize="12sp"
android:spinnerMode="dialog"
android:clickable="false"/>
Enjoy Coding
I dont think there is a color associated with the text. Its most likely predefined in the android code, might have to just make your own if you want to change the spinner's color.
This would include changing the ondraw() method and you draw the spinner how you would like it to look.
Only thing I think could potentially solve this issue is the style property of the spinner.
Source: http://developer.android.com/reference/android/widget/Spinner.html
This might help:
http://www.designerandroid.com/?p=28
Can change the text colour by overriding the getView method as follows:
new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_dropdown_item, list()){
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
//change the color to which ever you want
((CheckedTextView) view).setTextColor(Color.RED);
return view;
}
this worked for me create your own layout file with a custom definition for the spinner item.
custom_spinner.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/txt_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#ffffff" />
Change the spinner item using adapter
Spinner spinner= (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.by_loc_array,R.layout.txt_spinner);
spinner.setAdapter(adapter);
精彩评论