2014-01-09 28 views
2

我有一排按鈕,我希望它們全都顯示在一行中,如果轉向橫向,然後適合儘可能多的時候,可以將肖像和其他按鈕移動到下面。目前這些按鈕只是在一排擠壓在一起。按鈕都擠在一排

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:gravity="center_horizontal"> 

<Button 
    android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:background="@drawable/analoguebutton" /> 

<Button 
    android:id="@+id/Button03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:background="@drawable/analoguebutton" /> 

<Button 
    android:id="@+id/Button04" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp" 
    android:background="@drawable/analoguebutton" /> 

All squashed

+0

以下文章應該會有所幫助。 http://stackoverflow.com/questions/14528381/android-horizo​​ntal-linearlayout-wrap-elements – mcsilvio

回答

2

讓兩個佈局具有相同的名稱。
將「垂直」一個放入res/layout-port和「水平」一個放入res/layout-land
設計兩種佈局以符合您的口味。
Android會根據設備的方向自動選擇此佈局或佈局。

2

只需在所有按鈕中將android:layout_width="wrap_content"更改爲android:layout_width="match_parent",並將android:layout_weight="1"全部設置爲三個按鈕。希望它有效。