简体   繁体   中英

how to change the color of string array resources in android

This is my menu_items.xml whose elements are shown in a listview. They are Black coloured by default. I want them to be white. How can in change the color of these string-array items? Below is my menu_items.xml code.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="menu_items" >
        <item >Demo Mode</item>
        <item >Inbox</item>
        <item >Sent Items</item>
        <item >Filing History</item>
    </string-array>
</resources>   

Have your string items as :

   <string-array name="menu_items" >
        <item >Demo Mode</item>
        <item >Inbox</item>
        <item >Sent Items</item>
        <item >Filing History</item>
    </string-array>

   <string-array name="menu_items_labels" >
        <item ><FONT COLOR="#006600"><b>Demo Mode</b></FONT></item>
        <item ><FONT COLOR="#006600"><b>Inbox</b></FONT></item>
        <item ><FONT COLOR="#006600"><b>Sent Items</b></FONT></item>
        <item ><FONT COLOR="#006600"><b>Filing History</b></FONT></item>
      </string-array>

In your spinner code - use:

// For setting the html code "menu_items_labels" with font color white
Spinner spinner = (Spinner) findViewById(R.id.my_spinner);
spinner.setAdapter(
      new ArrayAdapter<CharSequence>(this, 
         R.layout.multiline_spinner_dropdown_item, 
        myActivity.this.getResources().getTextArray(R.array.menu_items_labels)));

To retrieve the String value (Without HTML Formatting),

strTemp = getResources().getStringArray(R.array.menu_items)[spinner.getSelectedItemPosition()];

You can not directly tell the (array) resource which color it should have. You either can define an app-wide theme/style to use for list items or create an Adapter with custom view, in your case an ArrayAdapter will fit best.

Hot to create an ArrayAdapter with a custom view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM