尝试将常规Button和ImageButton设置为相同的值时,LinearLayout中出现一些奇怪的行为layout_weight
。图像按钮的android:src
资源非常小,非常适合ImageButton内部使用,没有任何问题。尽管它们的宽度应该相同,但由于某些原因,ImageButton的宽度大约是其余普通按钮的大小的2 / 3rds。如果我将ImageButton乘以layout_weight
10,则其尺寸更接近正确的大小,但是为什么不起作用?
<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>
您需要android:layout_width="0dp"
为所有按钮设置相同的宽度。(更准确地说,您需要layout_width
为所有这些按钮设置相同的值,并且0dp
是要使用的常规宽度。当使用相等的权重时,只要仍有更多的空间可以分配,则实际使用的宽度与结果无关。 )layout_weight
唯一决定在按钮占据其指定宽度之后如何分配剩余空间。因此,如果它们开始时不相等(宽度为wrap_content
),那么在考虑其权重之后它们将保持不同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句