gpt4 book ai didi

android - 包含使用合并标签和 ViewStub 的 View

转载 作者:太空狗 更新时间:2023-10-29 16:15:28 26 4
gpt4 key购买 nike

我想延长我的 TableLayout通过 ViewStab 的一些额外行元素。这是我的Activity的布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root_table_layout">

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 1"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 2"/>
</TableRow>

<ViewStub
android:id="@+id/view_stub_1"
android:layout="@layout/merge_table_1"/>

<ViewStub
android:id="@+id/view_stub_2"
android:layout="@layout/merge_table_2"/>

</TableLayout>

或者view_stub_1view_stub_2Activity 上膨胀onCreate (这取决于应用程序的条件)。

ViewStub应该膨胀用 <merge> 定义的布局标记为根元素。像这样:

<merge xmlns:android="http://schemas.android.com/apk/res/android">

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 5"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Row 6"/>
</TableRow>

</merge>

但是当Activity正要开始,我得到了这个异常:

merge can be used only with a valid ViewGroup root and attachToRoot=true - worked with getLayoutInflater

最佳答案

好像<merge> ViewStub 不支持标签现在的元素:

The only drawback of ViewStub is that it currently does not support the tag.

Romain Guy's post on layout tricks

最后我用了ActivityLayoutInflaterTableLayout 中包含其他行:

ViewGroup rootTableLayout = (ViewGroup) findViewById(R.id.root_table_layout);
getLayoutInflater().inflate(R.layout.merge_table_1, rootTableLayout, true);

因为我只想附上一些TableRowsViewGroup 的和,此解决方案有效。对于更复杂的场景(在 TableRows 中间动态包含 TableLayout),我仍然不知道什么是正确的方法。

关于android - 包含使用合并标签和 ViewStub 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344787/

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