0

layout_weight = "1"및 layout_width = "0dp"를 사용하여 균등하게 간격을 두는 4 개의 버튼이 있습니다. 그러나 대형 태블릿 레이아웃에서는 버튼이 너무 넓어 져보기가 좋지 않으므로 모든 버튼에 대해 maxWidth를 설정하고 대신 전체 LinearLayout 양면에 빈 공간이 필요합니다 (따라서 네 개의 버튼이 가운데에 클러스터 됨). 그러나 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> 
    

    시도 최대 너비 내 버튼 폭 자신의 서브 레이아웃

0

은 당신이 원하는대로 이것`

<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

관련 문제