Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - How to change the text background color of a opened Spinner on Android

I set this XML Style in my app, but when I open a Spinner I cant see the text. This is where searching of information takes place.I don't really know what I am doing wrong.

This is the XML Style:

<style name="Theme.Color" parent="android:Theme">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:windowBackground">@drawable/fondo2</item>
</style>

This the XML of the Spinner:

            <Spinner
            android:id="@+id/spAnswers"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvSecurity"
            android:entries="@array/box" />

And when I open a Spinner this happens, I cant see the text (text color is white).

I need to set the text color to another of the spinner only when it is open or the background of the text changes.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While Creating Adapter for your Spinner give custom layout instead of predefined one

Create xml named spinner_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/cust_view"  
    android:layout_width="match_parent" 
    android:textColor="@color/black"
    android:textSize="12dp"
    android:layout_height="36dp" 
    android:gravity="left|center_vertical"/> 

Here you can change the color Text size and width and height of the Elements in the spinner by modifying this textview

Use it like this while creating Adapter

 ArrayAdapter<String> adapter=new ArrayAdapter<String>(context, R.layout.spinner_row,yourlist);

The Last task is routine

spinner.setAdapter(adapter);

I hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...