gpt4 book ai didi

android - 布局上的 "tag"和 "id"是什么?

转载 作者:IT老高 更新时间:2023-10-28 23:16:43 29 4
gpt4 key购买 nike

我知道 switch 语句是如何工作的,但我不知道这意味着什么(R.id.webbutton)。谁能解释一下它是什么以及什么是 TAG?有初学者的指南吗?我的意思是绝对的初学者。

最佳答案

IDs and Tags

ID

Views may have an integer id associated with them. These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree. A common pattern is to:

Define a Button in the layout file and assign it a unique ID.

<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>

From the onCreate method of an Activity, find the Button

Button myButton = (Button) findViewById(R.id.my_button);

View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.

标签

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

Tags may be specified with character sequence values in layout XML as either a single tag using the android:tag attribute or multiple tags using the child element:

 <View ...
android:tag="@string/mytag_value" />
<View ...>
<tag android:id="@+id/mytag"
android:value="@string/mytag_value" />
</View>

Tags may also be specified with arbitrary objects from code using setTag(Object) or setTag(int, Object).

关于android - 布局上的 "tag"和 "id"是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9757069/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com