首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏Android相关

    LinearLayout中的OnMeasure解析

    onMeasure-measureVertical方法 该方法会进行下面的几个步骤 声明使用变量 获取子View总高度 计算LinearLayout的高度 将子View中的Weight属性转换成高度,再重新

    81130发布于 2018-10-24
  • 来自专栏Android知识

    继承ViewGroup学习onMeasure和onLayout

    在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout。 实际上,view的测量工作在onMeasure(int, int)方法中完成。因此,只有onMeasure(int, int)方法可以且必须被重写。 3,解析onMeasure(int, int)方法 void android.view.View.onMeasure(int widthMeasureSpec, int heightMeasureSpec 调用父view的onMeasure(int, int)是合法有效的用法。 view的基本测量数据默认取其背景尺寸,除非允许更大的尺寸。 子view必须重写onMeasure(int, int)来提供其内容更加准确的测量数值。

    79540编辑于 2021-12-15
  • 来自专栏Android相关

    LinearLayout.onMeasure--事例说明

    将LinearLayout中代码Copy了一份存在本地,然后再在里面加了几个子View,打印出来LinearLayout.onMeasure中的那些变量的值 如下图所示,LinearLayout中有4

    63020发布于 2018-10-24
  • 来自专栏Android相关

    LinearLayout.onMeasure-声明变量

    mTotalLength:表示所有子View所需要的高度 maxWidth:表示这个LinearLayout的宽度,最后设置宽度的时候用到的 childState: alertnativeMaxWidth:没有Weight属性的子View中,最大的宽度 weightedMaxWidth :有weight属性的子View中,最大的宽度 allFillParent :是否所有的子View都是fillParent的 totalWeight:所有子View的weight和,到时候会用来计算剩余空间的分配 count:子View的总数 widthMode:宽度的MeasureSpec的Mode heightMode:高度的MeasureSpec的Mode matchWidth: baselineChildIndex: useLargestChild:是否用最高的子View大小 largestChildHeight:子View中最高的大小

    1.2K20发布于 2018-10-24
  • 来自专栏Android相关

    LinearLayout.onMeasure-获取子View总高度

    for (int i = 0; i < count; ++i) { final View child = getVirtualChildAt(i); if (child == null) { mTotalLength += measureNullChild(i); continue; } if (child.getVisibility() == View.GONE) {

    1.6K40发布于 2018-10-24
  • 来自专栏Android相关

    LinearLayout.onMeasure-设置LinearLayout的高度

    if (!allFillParent && widthMode != MeasureSpec.EXACTLY) { maxWidth = alternativeMaxWidth; } maxWidth += mPaddingLeft + mPaddingRight; // Check against our minimum width maxWidth = Math.max(maxWidth, getSuggestedMinimumWidt

    1.9K20发布于 2018-10-24
  • 来自专栏程思阳的专栏

    android组件调用onMeasure时报空指针

    如果这个View是通过LayoutInflater来构建的,会报: java.lang.NullPointerException at android.widget.RelativeLayout.onMeasure

    45920编辑于 2022-01-10
  • 来自专栏Android相关

    LinearLayout.onMeasure-Weight属性的转换

    // Either expand children with weight to take up available space or // shrink them if they extend beyond our current bounds int delta = heightSize - mTotalLength; if (delta != 0 && totalWeight > 0.0f) { float weightSum = mWeightSum > 0.

    69020发布于 2018-10-24
  • 来自专栏风吹杨柳

    android视图学习---从源码角度来理解onMeasure过程

    类的onMeasure总都做了什么? onMeasure方法了: [java] viewplaincopy 1.  /** 2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   5.         * 这个方法必须由onMeasure(int, int)来调用,来存储测量的宽,高值。 3.   */ 4.  而具体的测量任务就交给我们在子类中重写的onMeasure方法。 measureChildren() [java] viewplaincopy 1.  /** 2.

    75820发布于 2019-07-08
  • 来自专栏Android干货

    自定义控件详解(五):onMeasure()、onLayout()

    前言:   自定义控件的三大方法: 测量: onMeasure(): 测量自己的大小,为正式布局提供建议 布局: onLayout(): 使用layout()函数对所有子控件布局 绘制: onDraw (): 根据布局的位置绘图       onDraw() 里面是绘制的操作,可以看下其他的文章,下面来了解 onMeasure()和onLayout()方法。 一、onMeasure()、测量 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 参数即父类传过来的两个宽高的 : MeasureSpec.AT_MOST = 2 MeasureSpec.EXACTLY = 1 MeasureSpec.UNSPECIFIED = 0 上面我们知道了 onMeasure 这个方法和onMeasure()方法类似。其实这个方法的作用就是 设置当前View的宽高。   (2)、 ?       这个方法就和 ?

    89920发布于 2018-06-08
  • 来自专栏码客

    Android 自定义View中的onMeasure onLayout onDraw

    正文 Android自定义View时常重写三个方法onMeasure和onLayout以及onDraw。 他们的作用 onMeasure 计算当前View的宽高 onLayout 处理子View的布局 onDraw 绘制当前View 调用的顺序为onMeasure–>onLayout–>onDraw 就需要先调用requestLayout 再调用invalidate onMeasure细要 @Override protected void onMeasure(int widthMeasureSpec , int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 获取宽-测量规则的模式和大小 widthSize, mHeight); } else { setMeasuredDimension(widthSize, heightSize); } } 我们可以重写onMeasure

    2.9K10发布于 2019-10-22
  • 来自专栏大前端修炼手册

    Android自定义View中的onMeasure、onLayout和onDraw方法解析

    一、onLayout、onMeasure和onDraw方法 1.1 onMeasure(int widthMeasureSpec, int heightMeasureSpec) onMeasure方法用于测量 方法 在CircleView类中,重写onMeasure方法,根据MeasureSpec来计算并设置View的宽高。 通过这个案例,我们可以看到,onMeasure、onLayout和onDraw这三个方法在自定义View中的重要作用。 方法 在CustomLayout类中,重写onMeasure方法,根据MeasureSpec来计算并设置ViewGroup的宽高。 onMeasure方法用于测量View的大小,onDraw方法用于绘制View的内容,onLayout方法用于确定View的位置。

    1.4K10编辑于 2024-07-23
  • 来自专栏韩曙亮的移动开发专栏

    【错误记录】Android Studio 布局文件报错 ( View with id -1: xx.MyView#onMeasure() did not set the measured dime )

    整个 Design 图形化操作界面没了 , 报错信息如下 : java.lang.IllegalStateException: View with id -1: com.example.MyView#onMeasure () 方法 中 没有调用 setMeasuredDimension() 方法导致的 ; 实现 onMeasure() 方法时,需要 调用 setMeasuredDimension() 来设置 View 以下是一个示例,在这个示例中,自定义 View 的 onMeasure() 方法调用了 setMeasuredDimension() 来设置 View 的测量宽度和高度。 : View(context, attrs) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { 实现 onMeasure() 方法时,必须在最后 调用 setMeasuredDimension() 来设置视图的测量宽度和高度。

    1.2K10编辑于 2023-04-30
  • 来自专栏github@hornhuang

    Android 自定义控件 自定义 View 入门必备

    ---- 接下来让我们开启自定义控件之路 关于自定义控件,一般辉遵循一下几个套路 首先重写 onMeasure() 方法 其次重写 onDraw() 方法 总所周知 onMeasure() 方法是用来重新测量 所以这时就需要重写 onMeasure 方法,设定其宽高相等。 ---- 那么该如何重写 onMeasure() 方法呢? 首先把 onMeasure() 打出来 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 这时大家不眠会好奇,明明是重绘大小,那么给我提供宽高就行了呀 ---- 开始重写 onMeasure() 方法 首先,无论是 width 还是 height ,我们都得先判断类型,再去计算大小,so~ 咱先写个方法专门用于计算并返回大小。

    93850发布于 2021-09-24
  • 来自专栏全栈程序员必看

    Android中mesure过程详解 –[通俗易懂]

    = MEASURED_DIMENSION_SET) { throw new IllegalStateException("onMeasure() did not set 函数,因此真正有变数的是onMeasure函数,onMeasure的默认实现很简单,源码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec ,即可完成View的测量,当然你也可以重载onMeasure,并调用setMeasuredDimension来设置任意大小的布局,但一般不这么做,因为这种做法太“专政”,至于为何“专政”,读完本文就会明白 对于ViewGroup的子类而言,往往会重载onMeasure函数负责其children的measure工作,重载时不要忘记调用setMeasuredDimension来设置自身的mMeasuredWidth 如果我们在layout的时候不需要依赖子视图的大小,那么不重载onMeasure也可以,但是必须重载onLayout来安排子视图的位置,这在下一篇博客中会介绍。

    72610编辑于 2022-09-19
  • 来自专栏青蛙要fly的专栏

    Android技能树 — View小结

    onMeasure方法了。) 其实奥秘就在我们平时重写的onMeasure()方法中: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec ) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 复制代码 我们是不是看到了onMeasure方法里面传入了 onMeasure()方法的构成 我们前面提过,自定义View是要重写onMeasure()方法的,我们再仔细分析下: @Override protected void onMeasure(int widthMeasureSpec super.onMeasure(widthMeasureSpec,heightMeasureSpec); } } 复制代码 我们可以查看super.onMeasure方法: protected

    1K20发布于 2018-08-29
  • 来自专栏HenCoder

    HenCoder UI 部分 2-1 布局基础

    View 或 ViewGroup 的布局过程 测量阶段,measure() 方法被父 View 调用,在 measure() 中做一些准备和优化工作后,调用 onMeasure() 来进行实际的自我测量 onMeasure() 做的事,View 和 ViewGroup 不一样: View:View 在 onMeasure() 中会计算出自己的尺寸然后保存; ViewGroup:ViewGroup 在 onMeasure 布局过程自定义的方式 三类: 重写 onMeasure() 来修改已有的 View 的尺寸; 重写 onMeasure() 来全新定制自定义 View 的尺寸; 重写 onMeasure() 和 onLayout 第一类自定义的具体做法 也就是重写 onMeasure() 来修改已有的 View 的尺寸的具体做法: 重写 onMeasure() 方法,并在里面调用 super.onMeasure(),触发原有的自我测量 ; 在 super.onMeasure() 的下面用 getMeasuredWidth() 和 getMeasuredHeight() 来获取到之前的测量结果,并使用自己的算法,根据测量结果计算出新的结果

    47520发布于 2018-08-20
  • 来自专栏Android知识分享

    Carson带你学Android:手把手带你深入学习自定义View Measure过程

    具体流程 需要特别注意的是:若需进行自定义ViewGroup,则需重写onMeasure(),在下面的章节会详细讲解。 * 注:ViewGroup = 一个抽象类 = 无重写View的onMeasure(),需自身复写 **/ 根据上一小节可知,单一View的measure过程对onMeasure()有统一的实现 因此,ViewGroup无法对onMeasure()作统一实现。这个也是单一View的measure过程与ViewGroup的measure过程最大的不同。 复写onMeasure() 针对Measure流程,自定义ViewGroup的关键在于:根据需求复写onMeasure(),从而实现子View的测量逻辑。 此处主要分析的是LinearLayout的onMeasure(),具体如下所示。

    50510编辑于 2022-03-24
  • 来自专栏李蔚蓬的专栏

    3.2 自定义控件基础 之 View的测量

    View类默认的onMeasure()方法只支持EXACTLY模式,所以如果在自定义控件的时候不重写onMeasure()方法的话,就只能使用EXACTLY模式。 首先要重写onMeasure()方法,该方法如下所示。 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure 通过上面的分析,重写的onMeasure()方法代码如下所示。 当指定宽高属性为wrap_content时,如果不写onMeasure()方法,那么系统就不知道该使用默认多大尺寸。

    58530发布于 2018-09-13
  • 来自专栏分享达人秀

    手把手教你读懂源码,View的绘制流程详细剖析

    继续查看View类的onMeasure()方法: ? onMeasure方法 其实View类的onMeasure方法一般是由其子类来重写的。 如对于用来应用程序窗口的顶层视图的DecorView类来说,它是通过父类FrameLayout来重写祖父类View的onMeasure方法的,接下来我们就分析FrameLayout类的onMeasure 分析onMeasure方法,我们先从子类DecorView的onMeasure方法入手,这个方法主要是调整了两个入参高度和宽度,然后调用其父类的onMeasure方法。 ? DecorView的onMeasure方法 再看FrameLayout的onMeasure方法,主要是遍历所有的子View进行测量,然后设置高度、宽度。 ? View的绘制主流程 在measure方法中,会调用onMeasure方法,在onMeasure方法中会对所有的子元素进行measure过程,这个时候measure流程就从父容器传递给子容器,这样就完成了一次测量

    2.1K100发布于 2018-02-02
领券