包括来自其他布局android的linearlayout

时间:2013-02-25 09:13:24

标签: android xml layout

我在layout.xml中有一个android(R.layout.items)小组:

<?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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="textview1: " />

    <LinearLayout
        android:id="@+id/Panel1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:orientation="horizontal"
        android:padding="1dp" >

    <EditText
        android:id="@+id/agente2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:maxLength="5"
        android:singleLine="true" />

    <EditText
        android:id="@+id/agente2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:enabled="false"
        android:singleLine="true" >
    </EditText>
    <LinearLayout
        android:id="@+id/Panel2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="6dp"
        android:orientation="horizontal"
        android:padding="1dp" >
    <CheckBox
        android:id="@+id/cbTipoIncidente1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
.....

在此面板中,我有一些linearlayout我想要动态地以编程方式添加到另一个layout(r.layout.other)

通过充值,我只能添加r.layout资源,而不能添加r.id。我想将单个视图(例如panel1)添加到r.layout.other。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

LinearLayout保存为单独的panel.xml,然后使用layout.xml标记调用不同的<include>个文件,如下所示:

   <LinearLayout ... >
         <include layout="@layout/panel" />
  </LinearLayout>

答案 1 :(得分:2)

使用此概念

LinearLayout global_panel = new LinearLayout (this);
        global_panel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        global_panel.setGravity(Gravity.FILL);
        global_panel.setBackgroundResource(R.drawable.back);
相关问题