Android - 如何在布局中存储额外的隐藏信息(通过标签)
Android - How to store an extra hidden information(s) (by tag) in layout
我有这样的布局-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Linearlaout which acts as ListView in this case -->
<LinearLayout
android:id="@+id/Main_View_Reference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:contentDescription="This is a container reference for main list items"
</LinearLayout>
</ScrollView>
还有另一种布局,用于膨胀此布局的 "LinearLayout" 部分,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/Level_3_Layout"
android:orientation="vertical" >
<TextView
android:id="@+id/Level_3_Item_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Itame Name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/Level_3_Item_Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Item Price"
android:textColor="@android:color/black"
android:textSize="13sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
</LinearLayout>
我想做的是为从第二部分创建的动态膨胀布局分配一个标签值。
所以,我想为每个创建的(膨胀的)布局设置额外的隐藏信息 Java 来存储一些额外的信息,以便下次我需要它们时可以检索它们 HTML的隐藏字段。
android有什么办法吗?
感谢您的帮助。
布局使用android:tag=""
,例如
....
<View
android:tag="YOUR_HIDDEN_THINGIE"
.../>
或调用视图后
View v = findViewBy(R.id.myview);// could be any view widget
v.setTag(myobject);
另请注意,当您在布局中使用 android:tag 时,当您的布局膨胀时,您的 Tag
将持续存在,您可以通过 v.getTag()
检索它
我不太确定您所说的 'dynamic' 是什么意思,但我假设您不仅要分配值,还要为您的 'hidden field' 分配密钥。
如果我的假设是正确的,那么你可以使用这个方法:
yourView.setTag(int id, Object object);
我知道你不愿意使用 setTag
但在这个用例中,我认为这是最适合做的事情。不过请注意 int id
,使用它您可以根据需要分配任意标签(只要它们都具有不同的 ID),从而使其非常动态。
如果您想检索使用上述方法分配的标签,请调用:
yourView.getTag(int id);
希望对您有所帮助!
如果这仍然不是您想要的,恐怕唯一的选择是扩展您想要的 View
,然后根据您的需要对其进行自定义。
我有这样的布局-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Linearlaout which acts as ListView in this case -->
<LinearLayout
android:id="@+id/Main_View_Reference"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:contentDescription="This is a container reference for main list items"
</LinearLayout>
</ScrollView>
还有另一种布局,用于膨胀此布局的 "LinearLayout" 部分,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/Level_3_Layout"
android:orientation="vertical" >
<TextView
android:id="@+id/Level_3_Item_Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Itame Name"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:id="@+id/Level_3_Item_Price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
android:text="Item Price"
android:textColor="@android:color/black"
android:textSize="13sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray" />
</LinearLayout>
我想做的是为从第二部分创建的动态膨胀布局分配一个标签值。
所以,我想为每个创建的(膨胀的)布局设置额外的隐藏信息 Java 来存储一些额外的信息,以便下次我需要它们时可以检索它们 HTML的隐藏字段。
android有什么办法吗?
感谢您的帮助。
布局使用android:tag=""
,例如
....
<View
android:tag="YOUR_HIDDEN_THINGIE"
.../>
或调用视图后
View v = findViewBy(R.id.myview);// could be any view widget
v.setTag(myobject);
另请注意,当您在布局中使用 android:tag 时,当您的布局膨胀时,您的 Tag
将持续存在,您可以通过 v.getTag()
我不太确定您所说的 'dynamic' 是什么意思,但我假设您不仅要分配值,还要为您的 'hidden field' 分配密钥。 如果我的假设是正确的,那么你可以使用这个方法:
yourView.setTag(int id, Object object);
我知道你不愿意使用 setTag
但在这个用例中,我认为这是最适合做的事情。不过请注意 int id
,使用它您可以根据需要分配任意标签(只要它们都具有不同的 ID),从而使其非常动态。
如果您想检索使用上述方法分配的标签,请调用:
yourView.getTag(int id);
希望对您有所帮助!
如果这仍然不是您想要的,恐怕唯一的选择是扩展您想要的 View
,然后根据您的需要对其进行自定义。