如何从android:tag读取@array



我在标记属性中引用了一个字符串数组。我如何阅读它才能拿到阵列?

我的标签属性布局:

<Button android:id="@+id/btn" ... android:tag="@array/colors" />

我的阵列.xml:

<string-array name="colors">
    <item>red</item>
    <item>yellow</item>
    <item>green</item>
</string-array>

通过这种方式我获取标签(不幸的是作为对象):

Button btn = (Button) findViewById(R.id.btn);
Object tag = btn.getTag();
Log.d("COLORS", "tag: " + tag.toString());

日志输出为:

03-06 13:34:12.765: DEBUG/COLORS(2957): tag: @2131099648

当我仔细查看我的R.java:时

public static final int colors=0x7f060000;

这个十六进制值正是:

2131099648

因此,不知何故,必须有一种方法来获取这个对象,并将资源标识符作为int.

代替

String[] colors = getResources().getStringArray(R.array.colors);

我想做这样的事情:

String[] colors = getResources().getStringArray((int)tag);

谢谢你在这方面帮助我,

Marco

res/values文件夹中使用colors.xml,然后可以使用:

<resources>
    <color name="solid_red">#f00</color>
    <color name="solid_blue">#0000ff</color>
    <color name="solid_green">#f0f0</color>
    <color name="solid_yellow">#ffffff00</color>
</resources>

然后你可以使用这样的东西:

<Button android:id="@+id/btn" ... android:tag="@color/solid_red" />

最新更新