例如,如果我定义了一个方向为垂直的根线性布局:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
<!-- I would like to add content here dynamically.-->
</LinearLayout>
在根线性布局内,我想添加多个子线性布局,每个子线性布局方向是水平的。有了所有这些,我最终可以得到一个类似表格的输出。
例如根与child布局如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_root"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
<!-- 1st child (1st row)-->
<LinearLayout
...
android:orientation="horizontal">
<TextView .../>
<TextView .../>
<TextView .../>
</LinearLayout>
<!-- 2nd child (2nd row)-->
...
</LinearLayout>
由于子线性布局的数量及其内容非常动态,因此我决定以编程方式将内容添加到根线性布局。
如何以编程方式将第二个布局添加到第一个布局,这也可以为每个子级设置所有布局属性并在子级中添加更多其他元素?
In your onCreate()
,写出以下内容
LinearLayout myRoot = (LinearLayout) findViewById(R.id.my_root);
LinearLayout a = new LinearLayout(this);
a.setOrientation(LinearLayout.HORIZONTAL);
a.addView(view1);
a.addView(view2);
a.addView(view3);
myRoot.addView(a);
view1
, view2
and view3
是你的TextView
s。它们可以轻松地以编程方式创建。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)