在 xml 中使用 android:tag 参数? [英] Use of android:tag parameter in xml?

查看:50
本文介绍了在 xml 中使用 android:tag 参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了一个 xml 布局,它有一个 textView 如下

I saw a xml layout which has a textView as below

       <TextView
            android:id="@+id/tvHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Change password"
            android:tag="@string/regular"
            android:textAllCaps="true"
            android:textColor="@color/header_color"
            android:textSize="@dimen/font30" />

我想知道 android:tag 属性的用途是什么.不胜感激的详细回答.

I want to know what is android:tag property is used for. A detailed answer for be greatly appreciated.

注意 - 我读到它用于数据绑定,但无法遵循上下文.

Note - I read that it is used for data binding, but could not follow the context.

更新 1 -(感谢 Blackbelt 的回答)用法1. 可用于 xml 元素与活动的编译时绑定.虽然android推荐使用id.2. 可用于识别列表适配器的元素.例如,具有多种排序支持的列表适配器的特殊情况.然后我们可以使用 tag 元素来标识所需的列表项.

Update 1 - (Thanks Blackbelt for the answer) Usages 1. Could be used for compile time binding of xml elements with the activity. Although android recommends the use of id. 2. Could be used for identifying elements for list adapter. For eg, a special case of a list adapter with multiple sort support. Then we could use the tag element to identify the desired list item.

我很高兴知道它是否也可以用于分配任何其他财产?

I would be glad to know if it could also be used for assigning any other property?

推荐答案

我想知道 android:tag 属性是用来做什么的

I want to know what is android:tag property is used for

添加与视图对象本身相关联的附加信息是一种不同的方式.在你的情况下,你的 tag 是一个 string,但它也可以存储复杂的对象.例如,标签可以是一个模型类集,并在运行时使用 setTag()/getTag() 对进行检索.这些信息的使用方式取决于用户.Android 允许 findViewWithTag太.例如,在您的情况下,您可以使用 findViewById(R.id. tvHeaderTitle); 或使用 findViewWithTag(getString(R.string.regular)); 查找相同的对象> .一个使用示例是 ListView,当您的项目有一个按钮部分时,单击时您想知道数据集中的哪个项目与该行相关联.使用 setTag/getTag 这对很容易实现

it is a different way to add associate additional information with the view object itself. In you case your tag is a string, but it can store complex objects too. For instance the tag could be a model class set and retrieve at runtime using the setTag()/getTag() pair. The way this information is used is up to the users. Android allows to findViewWithTag too. In your case, for instance you could look for the same object using findViewById(R.id. tvHeaderTitle); or using findViewWithTag(getString(R.string. regular)); . An example of usage is with ListView when you have a button part of you item and, upon click you want to know the which item in your dataset is associated with that row. Using the pair setTag/getTag this thing is easily achievable

这篇关于在 xml 中使用 android:tag 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆