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

    LinearLayout中的OnMeasure解析

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

    86430发布于 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)来提供其内容更加准确的测量数值。

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

    LinearLayout.onMeasure--事例说明

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

    71520发布于 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.4K20发布于 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.7K40发布于 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

    2.1K20发布于 2018-10-24
  • 来自专栏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.

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

    android组件调用onMeasure时报空指针

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

    51020编辑于 2022-01-10
  • 来自专栏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)、 ?       这个方法就和 ?

    96020发布于 2018-06-08
  • 来自专栏风吹杨柳

    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.

    84520发布于 2019-07-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

    3K10发布于 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.7K10编辑于 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.3K10编辑于 2023-04-30
  • 来自专栏全栈程序员必看

    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来安排子视图的位置,这在下一篇博客中会介绍。

    79610编辑于 2022-09-19
  • 来自专栏分享达人秀

    手把手教你读懂源码,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.2K100发布于 2018-02-02
  • 来自专栏项勇

    笔记50 | Android自定义View(一)

    () 动手重写onMeasure函数 onDraw() ---- 自定义View 首先我们要明白,为什么要自定义View? 自定义View我们大部分时候只需重写两个函数:onMeasure()、onDraw()。onMeasure负责对当前View的尺寸进行测量,onDraw负责把当前这个View绘制出来。 当然了,View类给了默认的处理,但是如果View类的默认处理不满足我们的要求,我们就得重写 onMeasure函数啦。 先看看onMeasure函数原型: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 参数中的 widthMeasureSpec ---- 动手重写onMeasure函数 上面讲了太多理论,我们实际操作一下吧,感受一下onMeasure的使用,假设我们要实现这样一个效果:将当前的View以正方形的形式显示,即要宽高相等,并且默认的宽高值为

    88340发布于 2018-06-19
  • 来自专栏中国Android研究院

    房价都降了!我们聊聊吧。

    拿今天要讲的自定义View来说,其实我们在写自定义View的时候,需要去重写系统为我们设计好的一系列方法(如下所示): onMeasure -> onLayout -> onDraw 这些方法。 那我们今天就先聊一聊第一步onMeasure。 2 盖房子的时候,我们先会确定一块地方,然后用工具量好建房子的地面范围,量好的这块地就是我们可以盖房子的范围。 这个过程与我们在自定义View的onMeasure过程是类似的,都是确定一块我们接下来工作的范围。 测量的过程中,我们不单会测量整个房子的大小,我们要综合考虑房子里面的房间格局和大小。 谷歌的工程师看到这一切,深深的吸了一口气,然后缓慢的吐出来,转身回到工位上,盯着想了很久的代码: protected void onMeasure() { // TODO 我该咋弄捏? 好了,onMeasure阶段就讲到这里了。下一节onLayout再见。拜拜~ 动动小手,关注一下不迷路哦~

    54530发布于 2019-01-09
  • 来自专栏编程思想之路

    Android6.0源码分析之View(二)--measure

    研究了俩星期硬是没有研究view的measure,接下来终于可以来好好研究研究了) 先总体分析一下view的measure,发现关于view的measure研究主要涉及到两个方法和一个类 两个方法是 onMeasure 接下来对onMeasure进行一个分析,onMeasure属于生命周期方法,先来看一下onMeasure方法的实现与介绍。 Chapter Two,onMeasure方法简介 1>,onMeasure方法用于测量view以及其内容的宽高,得到一组宽和高的值measurewidth/heigh,在调用measure方法时会调用 (measure方法属于view的public方法),View的子类应该覆写onMeasure方法来提供一组准确有效的测量值。 方法重新进行测量,对于onMeasure方法的调用,有个版本界限,19版本以下是不论缓存区有没有存储值都会强制调用onMeasure,19版本以上不会。

    57120发布于 2019-05-28
  • 来自专栏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~ 咱先写个方法专门用于计算并返回大小。

    1K50发布于 2021-09-24
  • 来自专栏我就是马云飞

    View的绘制流程源码分析

    但是他内部通过调用onMeasure进行了测量。那么我们只需要看onMeasure的具体实现即可。 View的onMeasure代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension 不管你走不走onMeasure这个方法,它最后都会走setMeasuredDimensionRaw这个方法。也就是设置测量的宽和高。 这个方法看完了。 我们回到onMeasure中在看看其他方法到底处理了什么? ViewGroup的measure流程 在viewGroup没有onMeasure方法,但是有MeasureChildren方法。

    86760发布于 2018-02-05
领券