开发者

How do I change the text style of a spinner?

I'm creating a spinner in my layout xml files and setting an string array to this spinner. If I change the textstyle of the spinner the text is not affected by the changes.

I read in the googlegroups that a spinner has no text and therefore the textstyle开发者_StackOverflow社区 can not be changed and I have to change the style of the textview that is shown in the spinner. But how can I do that. Preferably in my xml file.


As my predecessor specified, you can't do it on the main XML layout file where the Spinner component is.

And the answer above is nice, but if we want to use Google's best practices, like you know... to use styles for everything... you could do it in 3 'easy' steps as follows:

Step 1: You need an extra file under your layout folder with the look for the Spinner's items:

<?xml version="1.0" encoding="utf-8"?>
<TextView  
    android:id="@+id/textViewSpinnerItem" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    style="@style/SpinnerTextViewItem"
    xmlns:android="http://schemas.android.com/apk/res/android" />

Name this file: spinner_item_text.xml

Step 2: Then, on your Activity Class when you are filling the Spinner with an array of items:

adapter = new ArrayAdapter<CharSequence>(this, R.layout.spinner_item_text, items);
spinner.setAdapter(adapter);

Note that the R.layout.spinner_item_text resource is in your own R's file.

Step 3: Under your values folder, create or use (you might have one already) the file styles.xml. The style entry needed should look like this one:

<style name="SpinnerTextViewItem" parent="@android:style/Widget.TextView" >
    <item name="android:textSize" >8dp</item>
    <item name="android:textStyle" >bold</item>
</style>

And that's it!

So far it has been really, really handy to put all about text sizes, styles, colors, etc... on a styles.xml file so it's easy to maintain.


Via XML Only

As a follow-up to @Cyril REAL's excellent answer, here is a thorough implementation of how to style your Spinners just through XML if you're populating your Spinner via android:entries.

The above answers work if you're creating your Spinner via code but if you're setting your Spinner entries via XML, i.e. using android:entries, then you can adjust the text size and other attributes with the following two theme settings:

In your res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppBaseTheme" parent="android:Theme.Holo">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

        <!-- For the resting Spinner style -->
        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item> 

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>

    </style>

    <style name="spinnerItemStyle">
        <item name="android:padding">10dp</item>
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="spinnerDropDownItemStyle">
        <item name="android:padding">20dp</item>
        <item name="android:textSize">30sp</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

</resources>


When you create the Adapter that backs the Spinner you can set a layout for the spinner item.

spinner.setAdapter(new ArrayAdapter(this, R.id.some_text_view));

You can style some_text_view the way you want.

<TextView android:id="@+id/some_text_view" android:textStyle="bold" />


Actually you can customize spinner's text by xml.

In your own style, define a :

<item name="android:spinnerItemStyle">@style/yourStyleForSpinnerItem</item>

and define this style also :

<style name="yourStyleForSpinnerItem">

  // Stuff you want for the item style.

</style>

When you instantiate the spinner's adapter in the java code, you can use the default android.R.layout.simple_spinner_item


If you don't want such workarounds, there's a simple way. Get the textview from the spinner and change its parameters:

TextView tv = (TextView) spin.getSelectedView();
tv.setTypeface(Typeface.DEFAULT_BOLD); //to make text bold
tv.setTypeface(Typeface.DEFAULT); //to make text normal


if you want just change background of popup View in spinner, call this method:

spinner.setPopupBackgroundResource(R.color.pa_md_white);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜