2014-04-10 103 views
0

我想在中間使用三個按鈕進行佈局。我打算再添加右側角球2個按鍵,但現在我試圖使其與這3個使所有按鈕具有相同的尺寸

這項工作是我腦子裏想的:

My idea

在某些方面,它應該在每個屏幕尺寸都相似(所以隨着屏幕尺寸變小,按鈕變小)。

現在,這是我所:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/multiplay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:background="@drawable/menubutton" 
    android:text="@string/multiplayer" /> 

<Button 
    android:id="@+id/options" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/multiplay" 
    android:layout_below="@+id/multiplay" 
    android:background="@drawable/menubutton" 
    android:text="@string/options" /> 

<Button 
    android:id="@+id/singleplay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/multiplay" 
    android:layout_alignLeft="@+id/multiplay" 
    android:background="@drawable/menubutton" 
    android:text="@string/singleplayer" 
/> 

看起來好像沒什麼大屏幕上,但在一個較小的這種情況:

On very small screen

我身邊有一派,和大多數頁面建議使用「重量」,但顯然只適用於線性佈局。有什麼建議?

感謝

+0

什麼是不使用線性佈局的原因是什麼? –

+0

爲此使用線性更好嗎? – Marshall

+0

考慮到你只是試圖把3個按鈕放在eachother之上,是的,最好使用LinearLayout。您仍然可以使用RelativeLayout作爲整體佈局(這樣您可以稍後在右上角添加這些按鈕),但在中間使用3個按鈕的LinearLayout。 –

回答

1

試試這個..

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/multiplay" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/menubutton" 
      android:text="@string/multiplayer" /> 

     <Button 
      android:id="@+id/options" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/menubutton" 
      android:text="@string/options" /> 

     <Button 
      android:id="@+id/singleplay" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/menubutton" 
      android:text="@string/singleplayer" /> 
    </LinearLayout> 

</RelativeLayout> 
1

應使用重量,這樣一來就可以均勻地devide屏幕上的按鈕,無論什麼屏幕尺寸。

這裏是RelativeLayout中的LinearLayour示例。 (如果在RelativeLayout中沒有其他的東西可以用LinearLayout替換RelativeLayout)

你應該玩一點它,以符合你的要求。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="3" > 

<Button 
    android:id="@+id/multiplay" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/menubutton" 
    android:text="@string/multiplayer" /> 

<Button 
    android:id="@+id/options" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/menubutton" 
    android:text="@string/options" /> 

<Button 
    android:id="@+id/singleplay" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/menubutton" 
    android:text="@string/singleplayer" 
/> 

    </LinearLayout> 

</RelativeLayout> 
相關問題