安卓 View 设置宽高 wrap_content 属性无效的原因

为了清楚描述问题,我们拿 button 来举例

如果 Button 宽高设置 wrap_content 并设置了 background 为一个图片的时候,会出现宽高默认为图片的大小 wrap_content 无效的情况,针对这个问题我们查看 Button 的 测量过程,因为 Button 继承自 TextView ,所以我们看 TextView 的 onMeasure 方法,只贴出关键代码:

            // Check against our minimum width
            width = Math.max(width, getSuggestedMinimumWidth());

            if (widthMode == MeasureSpec.AT_MOST) {
                width = Math.min(widthSize, width);
            }

如果是 widthMode 不是 MeasureSpec.EXACTLY 情况下 ,width 是测量自身后的值,然后和 getSuggestedMinimumWidth() 取最大值
getSuggestedMinimumWidth() 是 View 的方法:

    /**
     * Returns the suggested minimum width that the view should use. This
     * returns the maximum of the view's minimum width
     * and the background's minimum width
     *  ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
     * <p>
     * When being used in {@link #onMeasure(int, int)}, the caller should still
     * ensure the returned width is within the requirements of the parent.
     *
     * @return The suggested minimum width of the view.
     */
    protected int getSuggestedMinimumWidth() {
        return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
    }

也就是说没有设置背景的情况下,返回自身宽度最小值;有背景的情况下,返回背景的最小宽度和自身最小宽度的最大值,背景的最小宽度的概念是 getIntrinsicWidth(),也就是说图片的话是图片自身的宽度,如果背景是 ColorDrawable 之类的没有原始宽度的时候返回 -1。

然后到 Button 的默认 style “Base.Widget.AppCompat.Button” 看一下

<style name="Base.Widget.AppCompat.Button" parent="android:Widget">
        <item name="android:background">@drawable/abc_btn_default_mtrl_shape</item>
        <item name="android:textAppearance">?android:attr/textAppearanceButton</item>
        <item name="android:minHeight">48dip</item>
        <item name="android:minWidth">88dip</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:gravity">center_vertical|center_horizontal</item>
    </style>

可见默认值 android:minHeight = 48dip , android:minWidth = 88dip。
最后回到上面的问题,可以得出 Button 的高度取决于自身宽高的最小值和背景的宽高。

  • 如果背景有原始高度的话,比如图片 BitmapDrawable等,最终的测量值取已测量的值、背景、默认最小宽高的三者最大值。

  • 如果背景没有原始高度的话,比如 ColorDrawable 等,最终的宽高为已测量的值、背景二者最大值。
    其他继承 View 的控件跟 button 大同小异。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值