android - 如何从android :tag?读取@array

标签 android arrays string reference tags

我在标签属性中引用了一个字符串数组。我怎样才能阅读它以掌握阵列?

我的带有标签属性的布局:

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

我的 arrays.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);

谢谢你帮我解决这个问题,

马可

最佳答案

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" />

关于android - 如何从android :tag?读取@array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9584073/

相关文章:

python - 在 Python 中从现有 2D 数组创建 3D 数组

string - 检测重叠范围并在 oracle 中纠正

android - 如何在通过 adb 启动应用程序时传递 Intent 数据

java - Android Studio Clicker 应用程序中的 SharedPreferences 不起作用

wpf - Windows8 或 wpf 中的颜色数组?

java - 循环字符串替换

c - 字符串序列分析器

java - Android - 释放位图内存

java - 如何正确使用 UUID.fromString 方法?

arrays - 你如何在 Ruby 中的循环迭代中刷新数组?