2

As the question states I simply want to add more than one tag to an XML View. For example, say I want to set an array of strings AND a separate string from my resources. I know how to do them individually but I want to know if there's a way of attaching more than one tag to a view directly within the XML code.

Edit:

My plan was to have a LinearLayout (l#1) that contained a dynamic amount of of a different LinearLayout (l#2) and within that View there would be a Spinner and an EditText. I need one tag for the hint of the EditText and the other for the array of strings to populate the Spinner. In the entire layout there are a multiple l#1 each using l#2 to populate it dynamically and each needing different hints and string arrays based on what they are used for.

My next idea was to add a integer as a tag to represent l#1 and and use a Switch/Case block in my code to populate the children of l#2 with the right hints and string arrays.

4

2 回答 2

3

我认为这在 XML 中是不可能的,但在代码中,您可以做的是创建一个自定义对象,该对象包含您需要的字符串并将其设置为标记。

class CustomTagObject {
    public List<Strings> strings;
    public String myString;
}

然后稍后

CustomTagObject tagObj = new CustomTagObject();
tagObj.strings = new ArrayList<Strings>("String 1", "String 2");
tagObj.myString = "String from resources";
view.setTag(tagObj);

如果您解释为什么要将这些物品作为标签保存,我也许可以帮助您找到替代方法?

于 2013-07-25T13:29:35.660 回答
0

上述解决方案有效,但使用错误(它将增加您管理键/值映射的额外开销)。

实现上述目标的更好方法是使用setTag允许您指定与值关联的 id 的重载方法。

方法签名:

public void setTag(int key, Object tag)
于 2017-08-03T04:25:50.390 回答