View->View测量布局中修改布局参数和onSizeChanged方法的调用关系

XML文件

<?xml version="1.0" encoding="utf-8"?>
<com.gallery20.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.gallery20.app.MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_blue_light"/>
    <com.gallery20.app.MyButton
        android:id="@+id/my_btn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"/>
</com.gallery20.app.MyLinearLayout>

自定义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 onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.d(TAG, "MyButton onSizeChanged")
    }

    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 onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.d(TAG, "MyLinearLayout onSizeChanged")
    }

    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 onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        Log.d(TAG, "MyView onSizeChanged")
    }

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

Activity代码

  • 修改View布局参数
const val TAG = "Yang"
class MainActivity : AppCompatActivity() {
    private var mViewGroup : LinearLayout ?= null
    private var mButton : Button ?= null
    private var mView : MyView?= 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", "<-------updateView first------->")
            mView?.updateView()
        }, 2000)
    }
}

// log
2024-06-10 03:01:14.362 22705-22705 Yang          D MyView onMeasure
2024-06-10 03:01:14.363 22705-22705 Yang          D MyButton onMeasure
2024-06-10 03:01:14.363 22705-22705 Yang          D MyLinearLayout onMeasure
2024-06-10 03:01:14.380 22705-22705 Yang          D MyView onMeasure
2024-06-10 03:01:14.380 22705-22705 Yang          D MyButton onMeasure
2024-06-10 03:01:14.380 22705-22705 Yang          D MyLinearLayout onMeasure
2024-06-10 03:01:14.381 22705-22705 Yang          D MyLinearLayout onSizeChanged
2024-06-10 03:01:14.381 22705-22705 Yang          D MyView onSizeChanged
2024-06-10 03:01:14.381 22705-22705 Yang          D MyView onLayout
2024-06-10 03:01:14.381 22705-22705 Yang          D MyButton onSizeChanged
2024-06-10 03:01:14.381 22705-22705 Yang          D MyButton onLayout
2024-06-10 03:01:14.381 22705-22705 Yang          D MyLinearLayout onLayout
2024-06-10 03:01:14.403 22705-22705 Yang          D MyLinearLayout onDraw
2024-06-10 03:01:14.403 22705-22705 Yang          D MyView onDraw
2024-06-10 03:01:14.403 22705-22705 Yang          D MyButton onDraw
2024-06-10 03:01:14.419 22705-22705 Yang          D MyButton onDraw
2024-06-10 03:01:16.303 22705-22705 yang           I <-------updateView first------->
2024-06-10 03:01:16.306 22705-22705 Yang          D MyView onMeasure
2024-06-10 03:01:16.306 22705-22705 Yang          D MyLinearLayout onMeasure
2024-06-10 03:01:16.306 22705-22705 Yang          D MyView onSizeChanged
2024-06-10 03:01:16.306 22705-22705 Yang          D MyView onLayout
2024-06-10 03:01:16.306 22705-22705 Yang          D MyButton onLayout
2024-06-10 03:01:16.306 22705-22705 Yang          D MyLinearLayout onLayout
2024-06-10 03:01:16.307 22705-22705 Yang          D MyLinearLayout onDraw
2024-06-10 03:01:16.307 22705-22705 Yang          D MyView onDraw
2024-06-10 03:01:16.312 22705-22705 Yang          D MyView onDraw
  • 总结
    • View首次赋予布局参数
      • ViewonMeasure 的方法在父ViewGrouponMeasure之前执行
      • ViewGrouponSizeChanged在子ViewonSizeChanged之前执行
      • ViewonLayout 的方法在父ViewGrouponLayout之前执行
      • ViewGrouponDraw在子ViewonDraw之前执行
    • View第二次赋予布局参数
      • ViewonMeasure 的方法在父ViewGrouponMeasure之前执行
      • 只调用要修改布局参数的子View的onSizeChanged方法
      • ViewonLayout 的方法在父ViewGrouponLayout之前执行
      • ViewGrouponDraw在子ViewonDraw之前执行
  • 不修改View布局参数调用requestLayout()
mMainHandler.postDelayed({
    Log.i("yang", "<-------requestLayout------->")
    mView?.requestLayout()
}, 2000)

// log
2024-06-10 03:43:36.762 22704-22704 yang                   I  <-------requestLayout------->
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyView onMeasure
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyLinearLayout onMeasure
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyView onLayout
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyLinearLayout onLayout
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyLinearLayout onDraw
2024-06-10 03:43:36.772 22704-22704 Yang                   D  MyView onDraw
  • 总结
    • 不修改View布局参数调用requestLayout()不会调用onSizeChanged
  • 不修改View布局参数调用forceLayout()
mMainHandler.postDelayed({
     Log.i("yang", "<-------forceLayout------->")
     mView?.forceLayout()
 }, 2000)

// log 
没有改变View的可见性、改变View的大小或位置,无任何Log输出
  • 总结
    • 不修改View布局参数调用forceLayout()不会调用onSizeChanged
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值