自定义View二篇,如何自定义一个规范的ViewGroup

简介: 自定义View二篇,如何自定义一个规范的ViewGroup

前言

在自定义View开篇,必须跨过的一道坎儿 中,我们介绍了自定义View的几种方式,以及如何实现一个规范的自定义View,上文中也说了,实现一个规范的自定义ViewGroup是一件比较困难的事情,因为要考虑的情况包含 本身的padding以及子view的margin 与 本身wrap_content 问题。

image.gif

如何实现一个规范的ViewGroup,以实现垂直布局的LinerLayout为例

    • 新建LinerLayoutView 继承自ViewGroup

    首先我们让LinerLayoutView 适应wrap_content的情况,在onMeasure中处理如下,同自定义View处理一样,不同的是我们需要计算子View宽高,代码如下所示:

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
        int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        int totalheight = 0;
        int totalWidth = 0;
        for (int i = 0; i < getChildCount(); i++){
            View childView = getChildAt(i);
            int childrWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();
            totalheight = totalheight + childHeight;
            totalWidth = Math.max(totalWidth,childrWidth);
        }
        if (heightMode == MeasureSpec.AT_MOST && widthMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(totalWidth,totalheight);
        }else if (heightMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(measureWidth,totalheight);
        }else if (widthMode == MeasureSpec.AT_MOST){
            setMeasuredDimension(totalWidth,measureHeight);
        }
    }

    image.gif

    我们需要调用下面方法来测量子view

    measureChildren(widthMeasureSpec,heightMeasureSpec);

    image.gif

    因为这里我们是垂直排列的,所以要循环计算view的总高度,wrap_content情况对应的宽度为子View最大的宽度,上面代码比较简单 我们主要来看onLayout方法。

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int totalHeight = 0;
        for (int i = 0; i < getChildCount(); i++){
            View childView = getChildAt(i);
            int childrWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();
            childView.layout(0,totalHeight,childrWidth,totalHeight + childHeight);
            totalHeight = totalHeight + childHeight;
        }
    }

    image.gif

    view.layout方法 是将view放置在什么地方,分别对应left、top、right、bottom四个点,这里我们需要注意的是,务必使用getMeasureWidth不能使用getWidth,因为前者是在测量的时候获取的,后者在布局完成之后才能获取到。

    在布局文件中 引用这个ViewGroup,并且添加两个子View,代码如下所示:

    <com.support.hlq.layout.LinerLayoutView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="kkko"
            android:textColor="@color/colorAccent" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="kkko"
            android:textColor="@color/colorAccent" />
    </com.support.hlq.layout.LinerLayoutView>

    image.gif

    运行结果如图所示,可以看到我们已经适配了wrap_content的情况

    image.gif

    考虑ViewGroup的padding问题

    上面代码,已经实现了最简单的垂直排列,我们给LinerLayoutView设置大小为40的边距,发现边距并没有生效,所以我们需要在onMeasure以及onLayout的方法中考虑padding问题。改写onMeasure方法如下:

    for (int i = 0; i < getChildCount(); i++){
        View childView = getChildAt(i);
        int childrWidth = childView.getMeasuredWidth();
        int childHeight = childView.getMeasuredHeight();
        totalheight = totalheight + childHeight;
        totalWidth = Math.max(totalWidth,childrWidth);
    }
    totalheight = totalheight + getPaddingTop() + getPaddingBottom();
    totalWidth = totalWidth + getPaddingLeft() + getPaddingRight();

    image.gif

    因为我们处理的是ViewGroup的边距,所以我们只需要对最终计算的高度和宽度分别加上上下边距 和左右边距即可,这里你可能会有疑问

    int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
    int measureHeight = MeasureSpec.getSize(heightMeasureSpec);

    image.gif

    为什么上面两处代码不需要加上边距呢,因为MeasureSpec获取到的大小是已经包含过padding,所以我们不需要处理通过MeasureSpec获取的宽高。

    接下来,我们修改layout方法

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int totalHeight = getPaddingTop();
        for (int i = 0; i < getChildCount(); i++){
            View childView = getChildAt(i);
            int childrWidth = childView.getMeasuredWidth();
            int childHeight = childView.getMeasuredHeight();
            childView.layout(0 + getPaddingLeft(),
                    totalHeight,
                    childrWidth + getPaddingLeft(),
                    totalHeight + childHeight + getPaddingTop());
            totalHeight = totalHeight + childHeight;
        }
    }

    image.gif

    left点我们加上getPaddingLeft,总高度由0修改为getPaddingTop,其他两点也分别加上边距布局即可,运行结果如下所示

    image.gif

    我们可以看出ViewGroup的边距已经生效了。

    考虑子View的Margin问题

    到这里 这个自定义的ViewGroup还是不够规范,不信我们来给第一个TextView设置下边距为20dp

    <com.support.hlq.layout.LinerLayoutView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark">
        <TextView
            android:layout_marginBottom="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="kkko"
            android:textColor="@color/colorAccent" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="kkko"
            android:textColor="@color/colorAccent" />
    </com.support.hlq.layout.LinerLayoutView>

    image.gif

    在这里,因为要获取到margin所以必须重写

    generateLayoutParams 方法和 generateDefaultLayoutParams 方法

    image.gif

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }
    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }
    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return new MarginLayoutParams(p);
    }

    image.gif

    重写上面三个方法后,我们才能获取margin参数,同样的我们首先在onMeasure中考虑子view边距

    for (int i = 0; i < getChildCount(); i++) {
        View childView = getChildAt(i);
        MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
        int childrWidth = childView.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
        int childHeight = childView.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
        totalheight = totalheight + childHeight;
        totalWidth = Math.max(totalWidth, childrWidth);
    }

    image.gif

    还要在获取宽高的时候加上对应的边距即可,同样还需要在onLayout方法中考虑子view 的边距问题,修改如下:

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int totalHeight = getPaddingTop();
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) childView.getLayoutParams();
            int childrWidth = childView.getMeasuredWidth() ;
            int childHeight = childView.getMeasuredHeight() ;
            childView.layout(getPaddingLeft() + lp.leftMargin,
                    totalHeight + lp.topMargin,
                    childrWidth + getPaddingLeft() + lp.leftMargin,
                    totalHeight + childHeight + getPaddingTop() + lp.topMargin);
            totalHeight = totalHeight + childHeight + lp.topMargin + lp.bottomMargin;
        }
    }

    image.gif

    在layout的时候考虑子view的边距,记得在计算总高度的时候 也要加上边距和下边距,运行结果如下图所示

    image.gif

    这样一来,我们就定义了一个比较规范的ViewGroup,加上我们上篇文章讲的自定义属性,相信大家都掌握了自定义View的方法了。

    目录
    相关文章
    |
    Android开发 Kotlin
    【错误记录】Kotlin 编译报错 ( Type mismatch: inferred type is String? but String was expected )
    【错误记录】Kotlin 编译报错 ( Type mismatch: inferred type is String? but String was expected )
    3509 0
    【错误记录】Kotlin 编译报错 ( Type mismatch: inferred type is String? but String was expected )
    |
    Java
    java 一对多、多对多关系示例
    java 一对多、多对多关系示例
    496 0
    |
    XML 前端开发 Android开发
    Android自定义View-入门(明白自定义View和自定义ViewGroup)
    为什么要自定义View? 主要是Andorid系统内置的View 无法实现我们的 需求,我们需要针对我们的业务需求定制我们想要的 View.
    204 0
    Android自定义View-入门(明白自定义View和自定义ViewGroup)
    |
    Shell Android开发
    哪怕不学Gradle,这些开发中的常见操作,你也值得掌握(下)
    Gradle 是每个 Android 同学都逃不开的一个话题。
    340 0
    |
    Android开发
    TextView未绘制情况下获取其宽高
    /** * 注:StaticLayout是android中处理文字换行的一个类,TextView源码中也是通过这个类实现换行的,使用这个类可以 * 在不进行TextView绘制的前提下得到TextView的宽高,这里我们只需要获取到高度即可,这个高度当然也可以通过post * 在run中获取,但是这样做会有一个问题,界面是先绘制显示然后再计算高度根据我们的逻辑来收缩TextView的高度,在列表中 * 会出现闪烁的问题。
    2920 0
    |
    5天前
    |
    人工智能 JavaScript Linux
    【Claude Code 全攻略】终端AI编程助手从入门到进阶(2026最新版)
    Claude Code是Anthropic推出的终端原生AI编程助手,支持40+语言、200k超长上下文,无需切换IDE即可实现代码生成、调试、项目导航与自动化任务。本文详解其安装配置、四大核心功能及进阶技巧,助你全面提升开发效率,搭配GitHub Copilot使用更佳。
    |
    7天前
    |
    存储 人工智能 自然语言处理
    OpenSpec技术规范+实例应用
    OpenSpec 是面向 AI 智能体的轻量级规范驱动开发框架,通过“提案-审查-实施-归档”工作流,解决 AI 编程中的需求偏移与不可预测性问题。它以机器可读的规范为“单一真相源”,将模糊提示转化为可落地的工程实践,助力开发者高效构建稳定、可审计的生产级系统,实现从“凭感觉聊天”到“按规范开发”的跃迁。
    869 13
    |
    3天前
    |
    云安全 安全
    免费+限量+领云小宝周边!「阿里云2026云上安全健康体检」火热进行中!
    诚邀您进行年度自检,发现潜在风险,守护云上业务连续稳健运行
    1169 1
    |
    5天前
    |
    人工智能 JavaScript 前端开发
    【2026最新最全】一篇文章带你学会Cursor编程工具
    本文介绍了Cursor的下载安装、账号注册、汉化设置、核心模式(Agent、Plan、Debug、Ask)及高阶功能,如@引用、@Doc文档库、@Browser自动化和Rules规则配置,助力开发者高效使用AI编程工具。
    742 4
    |
    6天前
    |
    消息中间件 人工智能 Kubernetes
    阿里云云原生应用平台岗位急招,加入我们,打造 AI 最强基础设施
    云原生应用平台作为中国最大云计算公司的基石,现全面转向 AI,打造 AI 时代最强基础设施。寻找热爱技术、具备工程极致追求的架构师、极客与算法专家,共同重构计算、定义未来。杭州、北京、深圳、上海热招中,让我们一起在云端,重构 AI 的未来。