自定义View-onMeasure()方法

在android自定义View的时候经常需要复写View的onMeasure()方法,但是有些时候却并不需要。了解其中的原因有助于我们深刻掌握自定义view。
首先看一下View中onMeasure()方法的官方解释:

Measure the view and its content to determine the measured width and the measured height. This method is invoked by {@link #measure(int, int)} and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

大概意思就是该方法会测量View,从而决定View的大小,需要我们复写它。光这几句话很难明白其真正的用法。
当没有复写onMeasuer()方法时,我们使用march_parent和wrap_content效果一样,都是填充父控件。所以当我们自定义的View想自己定义大小,而又使用wrap_content时就需要复写onMeasure()。在复写的onMeasure()里根据你的需求重新的定义View的大小,然后通过:
setMeasuredDimension(measuredWidth, measuredHeight);
生效。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值