将内容添加到动态线性布局? [英] Adding content to a linear layout dynamically?

查看:150
本文介绍了将内容添加到动态线性布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我已经定义了一个根线的布局,其方向是垂直的:

If for example I have defined a root linear layout whose orientation is vertical:

的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>

在根线的布局,我想补充多的子线性布局的,每个孩子的线性布局方向是水平的。所有这些我可能最终像输出表。

Inside the root linear layout, I would like to add multiple child linear layouts, each of the child linear layout orientation is horizontal. With all these I could end up with a table like output.

例如根目录的孩子的布局,如:

For example root with child layout such as:

<?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>

由于孩子的线性布局,并对其内容的数量是相当活跃的,我决定将内容添加到根线布局编程。

Since the number of child linear layouts and their contents are quite dynamic, I've decide to add content to the root linear layout programmatically.

如何可以在第二布局​​被添加到第一编程,这也可能设置所有的布局属性为每个子和内部子添加更多的其他元素?

How can the second layout be added to the first programmatically, which could also set all the layout attributes for each child and add more other elements inside child?

推荐答案

在您的onCreate

In your onCreate

    LinearLayout my_root = (LinearLayout) findViewById(R.id.my_root);
    LinearLayout A = new LinearLayout(this);
    A.setOrientation(LinearLayout.HORIZONTAL);
    A.addView(view1);
    A.addView(view2);
    A.addView(view3);
    my_root.addView(A);

厂景,视图2,VIEW3是你textviews ..轻松编程方式创建

view1,view2,view3 are your textviews.. easily created programmatically

这篇关于将内容添加到动态线性布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆