Android-如何在布局中存储额外的隐藏信息(按标签)

Abr*_*hin 4 android android-layout android-layout-weight

我有这样的布局-

<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>
Run Code Online (Sandbox Code Playgroud)

还有另一个布局,用于像这样放大该布局的“ 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>
Run Code Online (Sandbox Code Playgroud)

我想要做的是我想为从第二部分创建的动态膨胀式布局分配标签值。

因此,我想为每个使用Java创建的(膨胀的)布局设置额外的隐藏信息,以存储一些额外的信息,以便下次需要它们(例如HTML的隐藏字段)时可以检索它们。

android有什么办法吗?

感谢您的帮助。

MrH*_*rio 5

我不确定您所说的“动态”是什么意思,但是我想您不仅要分配值,还要分配您的“隐藏字段”的密钥。如果我的假设正确,那么您可以使用此方法:

yourView.setTag(int id, Object object);
Run Code Online (Sandbox Code Playgroud)

我知道您不愿意使用,setTag但是在这种用例中,我认为这是最适合的操作。int id不过请注意,使用它可以根据需要分配may标签(只要它们都具有不同的ID),就可以使其变得非常动态。如果要检索使用上述方法分配的标签,请调用此方法:

yourView.getTag(int id);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

如果这仍然不是您想要的,恐怕唯一的选择就是扩展您View的需求,然后根据需要进行自定义。