2

일반 Buttons와 ImageButton을 같은 layout_weight으로 설정하려고하면 LinearLayout에서 이상한 동작이 나타납니다. 이미지 버튼의 android:src 리소스는 극도로 작으며 문제없이 ImageButton 내부에 잘 들어 맞습니다. 너비가 동일해야하지만, 어떤 이유로 ImageButton의 너비는 일반 버튼의 나머지 크기의 약 2/3입니다. ImageButton의 layout_weight에 10을 곱하면 크기에 훨씬 가깝지만 왜 작동하지 않습니까?Android ImageButton 및 Button의 layout_weight가 다른 너비로 이어지는 이유는 무엇입니까?

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_weight="1"> 
    <Button 
      android:id="@+id/clear" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/clear"/> 
    <Button 
      android:id="@+id/left_paren" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/left_paren"/> 
    <Button 
      android:id="@+id/right_paren" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/right_paren"/> 
    <ImageButton 
      android:id="@+id/backspace" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/backspace" 
      android:src="@drawable/backspace"/> 
</LinearLayout> 
+0

언제나 생각합니다. layout_width = "0dp". 안드로이드 대신 : layout_width = "wrap_content". layout_weight를 사용할 때마다. –

+0

http://stackoverflow.com/a/41451132/2219600 – amalBit

답변

4

는 동일한 폭해야 모든 버튼에 android:layout_width="0dp"을 설정해야합니다. (정확하게 말하자면, 모든 단추에 대해 동일한 layout_width을 설정해야하며, 사용하기위한 너비는 0dp입니다. 동일한 가중치를 사용하는 경우, 추가 여유 공간이있는 한 사용하는 실제 너비는 관련되지 않습니다. 결과입니다. layout_weight은 버튼이 할당 된 너비를 차지한 후에 의 나머지 공간이 할당 된 방법 만 결정합니다. 그래서 그들이 불평등 (그들은 폭이 wrap_content 일 것입니다)을 시작하면, 그들의 무게가 고려 된 후에도 달라집니다.

0

모든보기에 동일하게 설정하고 android:layout_width0dp으로 설정하여 모든보기에 동일한 너비의 부분을 적용해야합니다.

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/clear" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/clear" /> 

     <Button 
      android:id="@+id/left_paren" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/left_paren" /> 

     <Button 
      android:id="@+id/right_paren" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/right_paren" /> 

     <ImageButton 
      android:id="@+id/backspace" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/backspace" 
      android:src="@drawable/plus" /> 
    </LinearLayout>