2012-06-23 91 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