2017-09-16 20 views
0

私はLinearLayoutを4つのボタンで使用しようとしましたが、最後のボタンはカットされました。どのように私は、ボタンの幅がどのように私のボタンを編集することができます

<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> 

enter image description here すべてsmartphones'resolutionsに対応し得るために、私のコードを編集することができます layout_weight

を調整することにより、この均等に分割レイアウトを取得する

+0

android:layout_weight = "1" –

答えて

0

使用weightSumおかげで、一度これを試してみてください。

<?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

make width="0dp"ボタンや

0

まず、すべてのボタンに

android:layout_weight="1" 

を追加するには、LinearLayoutandroid:weightSum="4"を設定します。

Buttonandroid: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

関連する問題