0

layout_weight = "1"とlayout_width = "0dp"を使用して均等にスペースを取る4つのボタンがあります。しかし、大きなタブレットレイアウトではボタンが広がりすぎて醜いので、すべてのボタンのmaxWidthを設定し、代わりにLinearLayoutの両側に空白を入れたい(4つのボタンが中央に集中している)。しかし、StackOverflowを介してクロールすると、多くの人が一緒に動作しないと言います。私が上でやりたいことを達成する方法はありますか?layout_weightが均等に配置されていてもmaxWidthを持つスペーシングボタン

TL; DR:一定幅以下

  1. (例えば、100dp)は、すべての4つのボタンが等間隔に配置されています。
  2. レイアウトにボタンが100dpより大きい必要がある場合、4つのボタンはすべて100dpの幅に設定され、一緒に貼り付けられ、レイアウトの両側にスペースが残されます。

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="@dimen/layout_height" 
        android:background="@drawable/layout_background" 
        android:orientation="horizontal"> 
    
    <Button 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@color/button_background"/> 
    
    <Button 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@color/button_background"/> 
    
    <Button 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@color/button_background"/> 
    
    <Button 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:background="@color/button_background"/> 
    
    </LinearLayout> 
    

答えて

0

簡単なハックが

  1. はリニアレイアウトを使用して、ボタンをラップし、このレイアウト

  2. セットに重みを設定することができ、この

    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="@dimen/layout_height" 
        android:background="@drawable/layout_background" 
        android:orientation="horizontal"> 
    
    <LinearLayout 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:orientation="horizontal"> 
    <Button 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:maxWidth="100dp" 
        android:background="@color/button_background"/> 
    </LinearLayout>  
    
    </LinearLayout> 
    

    をお試しくださいt内のボタンまでの最大幅彼のサブレイアウト

0

は、あなたが望むように、この1 `

<Button 
    android:background="@color/button_background" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:layout_width="0dp" /> 

<Button 
    android:background="@color/button_background" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:layout_width="0dp" /> 

<Button 
    android:background="@color/button_background" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:layout_width="0dp" /> 

<Button 
    android:background="@color/button_background" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:layout_width="0dp" /> 

`

は、各ボタンにマージンを追加してみてください。

+0

あなたは答えを得ましたか? – DKV

関連する問題