2

通常のボタンと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" .instead: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_weightを設定し、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> 
関連する問題