简体   繁体   English

如何在Android中更改字符串数组资源的颜色

[英]how to change the color of string array resources in android

This is my menu_items.xml whose elements are shown in a listview. 这是我的menu_items.xml,其元素显示在列表视图中。 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. 以下是我的menu_items.xml代码。

<?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), 要检索字符串值(不使用HTML格式),

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. 您可以定义应用程序范围的主题/样式以用于列表项,也可以创建具有自定义视图的适配器,在这种情况下, ArrayAdapter最适合。

Hot to create an ArrayAdapter with a custom view. 热门使用自定义视图创建ArrayAdapter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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