为什么相同的layout_weight导致Android ImageButton和Button的宽度不同?

史努比·道格

尝试将常规Button和ImageButton设置为相同的值时,LinearLayout中出现一些奇怪的行为layout_weight图像按钮的android:src资源非常小,非常适合ImageButton内部使用,没有任何问题。尽管它们的宽度应该相同,但由于某些原因,ImageButton的宽度大约是其余普通按钮的大小的2 / 3rds。如果我将ImageButton乘以layout_weight10,则其尺寸更接近正确的大小,但是为什么不起作用?

<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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android:在Button或ImageButton上组合文本和图像

android:layout_weight是什么意思?

如何使用layout_weight使宽度和高度[正方形]大小相等?

了解权重和layout_weight时,layout_weight越大,布局中的收缩就越大。

使用weightSum和layout_weight时如何在LinearLayout元素上设置最小宽度

带有`layout_weight`和`maxWidth`的灵活水平布局

android- layout_weight =“ 0”的含义

为什么View中的layout_weight的行为与TextView的行为不同?

为什么在Chrome和Firefox中元素宽度不同?

RecyclerView中项目的layout_weight和layout_gravity不起作用

如何在RecyclerView项目中同时为垂直和水平创建layout_weight

使子项flex测量相同的高度和宽度的不同行

与宽度对齐时LinearLayout layout_weight =“ 1”需要单位

固定宽度的Android Layout_Weight不起作用

TableLayout,android:layout_weight,并包括

具有不同布局类型的layout_weight

为什么宽度不同?

具有RelativeLayout的android layout_weight

Android在垂直LinearLayout中将layout_weight设置为宽度

为什么 layout_weight 和 weightSum 对我不起作用?

为什么这两个宽度和高度相同的 HTML 画布返回第二个的高度不同?

如何在 Android 中定义 EditText 的不同颜色和宽度

Android:layout_weight 不适用于 Button 和 LinearLayout

对齐相同高度和不同宽度的图像

为什么 layout_weight 在 TextInputLayout 的孩子中不起作用?

Android LinearLayout layout_weight 行为

为什么 Canvas 在同一个 html 文档中的两个单独的 canvas 元素上以不同的方式绘制相同宽度和长度的相同图像?

为什么相同的算法在 C++ 和 Python 中会导致不同的输出?

具有相同宽度百分比的图像和按钮具有不同的宽度