如何在xml布局文件中为小部件设置整数标签? [英] How to set integer tag to a widget in xml layout file?
问题描述
我的布局简单,但只能设置字符串标签.如何设置整数标签?
I have simple layout, but I can only set string tag. How to set integer tag?
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="1"
android:src="@drawable/image" />
更新
UPDATE
我发现了如何在xml布局中设置Integer标签.我们需要在任何xml资源文件中指定一个整数变量.看起来应该像这样:
I found out how to set Integer tags in xml layout. We need to specify an integer variable in any xml resource file. That should look like that:
res/values/value.xml:
res/values/value.xml:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<integer name="int1">15</integer>
<integer name="int2">1</integer>
</resources>
现在我们可以自由使用"@ integer/int1"或"@ integer/int2"作为我们的xml小部件的标签,例如:
And now we are free to use "@integer/int1" or "@integer/int2" as tags for our xml widgets, for example:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tag="@integer/int2"
android:src="@drawable/image" />
但是,就我而言,我更喜欢以编程方式设置标签:)
However, in my case I preferred to set tag programmatically :)
推荐答案
在xml中,您只能设置String.但是在代码中您可以使用View.setTag(int value);
,因为它需要Object.要读取一个值,您需要将其强制转换为Integer int value = (Integer)view.getTag();
In xml you can only set String. But in code you can use View.setTag(int value);
because it takes Object. To read a value you need to cast it to Integer int value = (Integer)view.getTag();
这篇关于如何在xml布局文件中为小部件设置整数标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!