2014-02-21 72 views
1

我該如何讓一些按鈕像android這樣的圖片連續排列。
其實我需要添加或刪除按鈕之間的空間;按鈕必須穩定。 我總是在左邊有一個按鈕,右邊有一個按鈕,中間有一個按鈕,中間還有兩個按鈕。如果屏幕尺寸發生變化,之間的差距將改變。Android排列按鈕連續排列

預先感謝

這張照片顯示瞭如何則必須在小屏幕

enter image description here

和畫面兩個節目將如何在更大的屏幕工作。

enter image description here

按鍵 「1」 總是留給;
按鈕「5」總是正確的;
按鈕「3」總是居中;

問題:如何讓按鈕「2」和「4」始終與其他按鈕之間具有相同的空間?

+0

使用空間和layout_weight。 – njzk2

+0

(空間是一個視圖) – njzk2

+0

@ njzk2空間我的意思是圖片中的灰色空白(這些不是視圖);那些按鈕之間的距離:) –

回答

5

這裏我顯示了4個按鈕的配置。我們的想法是將按鈕換成水平方向LinearLayout,並在按鈕之間添加視圖layout_weight 1,以便在線性佈局更改大小時這些按鈕可以收縮和展開。

<LinearLayout 
    android:id="@+id/lh" 
    android:layout_width="208dp" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="30dp" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/editText2" 
     android:layout_below="@+id/editText2" 
     android:layout_marginTop="15dp" 
     android:text="B" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" > 
    </View> 

    <Button 
     android:id="@+id/Button01" 
     android:layout_width="30dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:text="B" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" > 
    </View> 

    <Button 
     android:id="@+id/Button02" 
     android:layout_width="30dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:text="B" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="1dp" 
     android:layout_weight="1" > 
    </View> 

    <Button 
     android:id="@+id/Button03" 
     android:layout_width="30dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="15dp" 
     android:text="B" /> 
</LinearLayout> 

或等價替代SpaceView傳達這一特定視圖的實際功能,但只適用於使用水平至少14的API。 Space是「輕量級View子類,可用於在通用佈局中的組件之間創建間隙」。

+0

擊敗我吧! :) –

+1

接下來是你的:) – user2314737

+2

謝謝;這就是我真正需要的; –