2013-03-04 68 views
0

我有下面的佈局,即1微調和線性佈局包含一些編輯框和微調'這個佈局映射/表示一個對象(讓我們稱之爲X)。 主微調將有n個條目,每個條目映射到佈局對象(X)。所以,我需要有n個佈局 我想允許用戶一次只填充1個對象,所以我只保留1個佈局可見。爲了解決這個問題,一種方法是在.xml中有n個佈局,並且在監聽器的選中狀態下播放可見性。是否還有其他更好/最優化的解決方法這個。 我怎樣才能使這種動態,即如果我最初不知道n的價值?Android動態線性佈局包含編輯框和微調

`<Spinner 
    android:id="@+id/linesspinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 


<LinearLayout 
    android:id="@+id/linView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/linename1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/linecffiltext" /> 

    <Spinner 
     android:id="@+id/trospinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/Tro_arrays" 
     android:prompt="@string/linetrotext" /> 

    <EditText 
     android:id="@+id/line1troval" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" > 
    </EditText> 

    <Spinner 
     android:id="@+id/cfspinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/cf_arrays" 
     android:prompt="@string/linecffiltext" /> 

    <EditText 
     android:id="@+id/line1cfval" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:ems="10" > 
    </EditText> 
</LinearLayout> 

回答

0

嘗試此操作,將LinearLayout放入另一個xml中,並在內部循環中多次調用該xml。 示例XML,名稱attrib_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<Linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

<TextView 
    android:id="@+id/lable" 
    android:layout_width="150dip" 
    android:layout_column="0" 
    android:padding="3dip" 
    android:textColor="#000033" /> 
</Linearlayout> 

代碼調用,在循環

Linearlayout row11 = (Linearlayout) LayoutInflater.from(this).inflate(
       R.layout.attrib_row, null); 
     ((TextView) row11.findViewById(R.id.lable)).setText("example"); 
+0

謝謝您response.With上述方法,線性佈局是不可見的screen.I試圖row11。 setVisibility(View.VISIBLE);也。 – user2094188 2013-03-04 11:47:15