View->View测量布局中requestLayout和forceLayout的区别

XML文件

<?xml version="1.0" encoding="utf-8"?>
<com.app.MyLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_ll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light"
    android:gravity="center"
    android:orientation="vertical">
    <com.app.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"/>
    <com.app.MyButton
        android:id="@+id/my_btn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"/>
</com.app.MyLinearLayout>

Activity文件

const val TAG = "Yang"
class MainActivity : AppCompatActivity() {
    private var mViewGroup : LinearLayout ?= null
    private var mButton : Button ?= null
    private var mView : View?= null
    private var mMainHandler = Handler(Looper.getMainLooper())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mViewGroup = findViewById(R.id.my_ll)
        mButton = findViewById(R.id.my_btn)
        mView = findViewById(R.id.my_view)
        mMainHandler.postDelayed({
            Log.i("yang", "<-------requestLayout------->")
            // 请求重新布局
            mView?.requestLayout()
        }, 2000)
    }
}

自定义View代码

class MyButton(context: Context, attrs: AttributeSet) : AppCompatButton(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d(TAG, "MyButton onMeasure")
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        Log.d(TAG, "MyButton onLayout")
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        Log.d(TAG, "MyButton onDraw")
    }
}

class MyLinearLayout(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d(TAG, "MyLinearLayout onMeasure")
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        Log.d(TAG, "MyLinearLayout onLayout")
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        Log.d(TAG, "MyLinearLayout onDraw")
    }
}

class MyView(context: Context, attrs: AttributeSet) : View(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        Log.d(TAG, "MyView onMeasure")
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        Log.d(TAG, "MyView onLayout")
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        Log.d(TAG, "MyView onDraw")
    }
    
    fun updateView() {
        val layoutParams = layoutParams
        layoutParams.height += 100
    }
}

requestLayout()方法

  • 不修改View的布局参数
 mMainHandler.postDelayed({
     Log.i("yang", "<-------requestLayout------->")
     // 请求重新布局
     mView?.requestLayout()
 }, 2000)

// log 
2024-06-09 08:42:24.081  4438-4438  yang                    I  <-------requestLayout------->
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onMeasure
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onMeasure
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onLayout
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onLayout
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyLinearLayout onDraw
2024-06-09 08:42:24.089  4438-4438  Yang                    D  MyView onDraw
  • 修改View的布局参数
mMainHandler.postDelayed({
    Log.i("yang", "<-------requestLayout------->")
    mView?.updateView()
}, 2000)

// log
2024-06-09 08:50:21.373  4943-4943  yang                 I  <-------requestLayout------->
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyView onMeasure
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyLinearLayout onMeasure
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyView onLayout
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyButton onLayout
2024-06-09 08:50:21.376  4943-4943  Yang                 D  MyLinearLayout onLayout
2024-06-09 08:50:21.377  4943-4943  Yang                 D  MyLinearLayout onDraw
2024-06-09 08:50:21.377  4943-4943  Yang                 D  MyView onDraw
2024-06-09 08:50:21.384  4943-4943  Yang                 D  MyView onDraw 

forceLayout()方法

  • 不修改View的布局参数
mMainHandler.postDelayed({
    Log.i("yang", "<-------forceLayout------->")
    // 请求重新布局
    mView?.forceLayout()
}, 2000)

// log 
没有改变View的可见性、改变View的大小或位置,无任何Log输出
  • 修改View的布局参数
mMainHandler.postDelayed({
    Log.i("yang", "<-------forceLayout------->")
    mView?.updateView()
}, 2000)

// log
2024-06-09 08:57:53.732  5625-5625  yang                   I  <-------forceLayout------->
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyView onMeasure
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onMeasure
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyView onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyButton onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onLayout
2024-06-09 08:57:53.741  5625-5625  Yang                   D  MyLinearLayout onDraw
2024-06-09 08:57:53.742  5625-5625  Yang                   D  MyView onDraw
2024-06-09 08:57:53.748  5625-5625  Yang                   D  MyView onDraw

总结

  • forceLayout()requestLayout()的主要区别在于影响范围:
    • forceLayout()将当前View标记为需要重新布局,但并不会立即触发布局过程。只有在下次draw(Canvas)方法被调用时,才会对其进行重新布局,没有改变View的可见性、改变View的大小或位置,不会调用onMeasureonLayoutonSizeChangedonDraw
    • requestLayout()将当前View以及其所有父View标记为需要重新布局。并不意味兄弟View也会被重新测量和布局。只有当这个View的父View的布局参数(如宽度、高度、权重等)发生变化,或者父ViewonLayout()方法被重写改变子View的布局方式时,兄弟View才可能会被重新测量和布局
    • 仅调用forceLayout()requestLayout()并不会影响到其兄弟View的测量和布局,除非父View的布局参数或布局方式发生了变化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值