问题
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" />
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.
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?
回答1:
I want to know what is android:tag property is used for
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
来源:https://stackoverflow.com/questions/35343138/use-of-androidtag-parameter-in-xml