2013-03-04 46 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