我想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>
Run Code Online (Sandbox Code Playgroud)
任何一个view_stub_1或被view_stub_2充气Activity 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>
Run Code Online (Sandbox Code Playgroud)
但是当Activity即将开始时,我得到了这个例外:
merge只能与有效的ViewGroup根和attachToRoot = true一起使用 - 与getLayoutInflater一起使用
现在看来元素<merge>不支持标记ViewStub:
ViewStub的唯一缺点是它目前不支持标记.
最后,我使用Activity的LayoutInflater包括附加行插入TableLayout:
ViewGroup rootTableLayout = (ViewGroup) findViewById(R.id.root_table_layout);
getLayoutInflater().inflate(R.layout.merge_table_1, rootTableLayout, true);
Run Code Online (Sandbox Code Playgroud)
因为我只想附加一些TableRows,所以ViewGroup这个解决方案有效.对于更复杂的场景(动态包含TableRows在a的中间TableLayout),我仍然不知道什么是正确的方法.
| 归档时间: |
|
| 查看次数: |
2283 次 |
| 最近记录: |