2012-06-23 150 views
0

我有一个动态布局,并在线性布局中添加组件。组件的数量也是动态的。我可以拥有布局的多布局方向吗?

当线性布局方向为水平时,水平添加项目,垂直添加垂直项目时。

现在,我可以首先水平垂直放置物品,然后垂直垂直放置物品。

或者我可以使用任何其他布局来满足我的需求。

像这样enter image description here

+0

嗨!你可以使用布局土地为新的XML文件...你可以阅读有关Android如何在这里提供这个http://developer.android.com/guide/topics/resources/providing-resources.html – Aamirkhan

+0

你可以使用layout-土地为新的xml文件...你可以阅读关于android如何在这里提供这个http://developer.android.com/guide/topics/resources/providing-resources.html – Aamirkhan

回答

1

你绝对可以有嵌套LinearLayout元素与多个方向。例如:

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

    <LinearLayout 
     android:id="@+id/firstRow" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" > 
     ... 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/secondRow" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" > 
     ... 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/thirdRow" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:orientation="horizontal" > 
     ... 
    </LinearLayout> 
</LinearLayout> 

使用这样的结构将允许您构建所描述的布局。

因为你可以做以下动态行:

layout_row.xml

<LinearLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:orientation="horizontal" /> 

MainActivity.java

mainLayout = (LinearLayout) findViewById(R.id.main_layout); 

// for each dynamic row you can inflate a new view and add it 
dynamicRow1 = getLayoutInflater.inflate(R.layout.layout_row, null); 
mainLayout.addView(dynamicRow1); 

dynamicRow2 = getLayoutInflater.inflate(R.layout.layout_row, null); 
mainLayout.addView(dynamicRow2); 

dynamicRow3 = getLayoutInflater.inflate(R.layout.layout_row, null); 
mainLayout.addView(dynamicRow3); 

在每一行中,你可以以编程方式添加动态您需要使用相同类型的逻辑创建视图。

+0

我的布局将是动态的。所以我不能提前定义行数。 – arnp

+0

这很公平。您可以轻松创建一个只有水平方向的“LinearLayout”的布局文件,以编程方式膨胀该布局,然后将其添加为主“LinearLayout”的子项。请参阅编辑代码示例。 – japino

+0

这很酷。谢谢。 – arnp