2013-02-25 112 views
0

我有一個layout.xml面板android(R.layout.items)包括來自其他佈局的Android的LinearLayout

<?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。有任何想法嗎?

回答

3

保存該LinearLayout作爲單獨panel.xml和使用<include>標籤如下不同layout.xml文件稱:

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

我不能每一個面板保存在其他的XML,因爲很多;是不是不可能從單個資源R.layout.items r.layout.other中添加dinamically到r.layout.other? – user2075861 2013-02-25 09:26:46

+0

這是調用通用佈局部分幷包含在其他xml文件中的方式。 – 2013-02-25 09:34:36

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);