2017-09-16 48 views
0

我試圖用LinearLayout有4個按鈕,但最後一個被切斷。如何編輯我的代碼,以獲得對應於所有smartphones'resolutions enter image description here如何編輯我的按鈕

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.2" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/red" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="RED" /> 

    <Button 
     android:id="@+id/blue" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="BLUE" /> 

    <Button 
     android:id="@+id/yellow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="YELLOW" /> 

    <Button 
     android:id="@+id/green" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="GREEN" /> 
</LinearLayout> 

感謝

按鈕的寬度
+0

採用Android:layout_weight = 「1」 –

回答

0

使用weightSum通過調整layout_weight

得到這個均分佈局一旦試試這個:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:weightSum="4" 
     android:gravity="center_horizontal" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/red" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="RED" /> 

     <Button 
      android:id="@+id/blue" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="BLUE" /> 

     <Button 
      android:id="@+id/yellow" 
      android:layout_width=match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="YELLOW" /> 

     <Button 
      android:id="@+id/green" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="GREEN" /> 
    </LinearLayout> 
</LinearLayout> 
0

使width="0dp"在所有按鈕並添加

android:layout_weight="1" 

所有按鈕

0

首先,在LinearLayout設置android:weightSum="4"

並在您的Button中設置android:layout_width="Odp"android:layout_weight="1"

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="4" 
    android:gravity="center_horizontal" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/red" 
     android:layout_width="Odp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="RED" /> 

    <Button 
     android:id="@+id/blue" 
     android:layout_width="Odp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="BLUE" /> 

    <Button 
     android:id="@+id/yellow" 
     android:layout_width="Odp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="YELLOW" /> 

    <Button 
     android:id="@+id/green" 
     android:layout_width="Odp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="GREEN" /> 
</LinearLayout> 
</LinearLayout> 
+0

@Lara的Fab,你可以檢查我的答案。 – KeLiuyue

相關問題