Android自定义ViewGroup:onMeasure与onLayout(1)

Wesley13
• 阅读 806

Android自定义ViewGroup:onMeasure与onLayout(1)

Android自定义一个ViewGroup,需要重写ViewGrouo里面的两个最重要的回调函数onMeasure()与onLayout()。如果开发者自己摆脱Android为我们做好的几套布局(如常见的线1性布局、相对布局、帧布局等等),往底层实现view呈现,那么我们就得在ViewGroup中小心计算和指定各个子view的位置和坐标。具体的,就是在onMeasure与onLayout里面,先计算出子view空间占据的位置,然后在onLayout里面layout()各个子view。
过程:
(1)写一个自定义的类继承自ViewGroup。
(2)在onMeasure()计算出ViewGroup占据的空间位置,其实就是宽和高。
(3)接着在onLayout()里面,在上一步计算框出的空间范围内一个一个的摆放layout子view。
本文以一个简化的例子说明,该例子实现常见的水平的线性布局,注意!本例出于简单演示的目的,仅仅实现一个水平的线性布局,如果是实现垂直的线性布局,那么细节地方的代码处理会不同,主要体现在测量高度和宽度及摆放的方向上。
写一个类MyLayout继承自ViewGroup,这个类名叫MyLayout还是MyViewGroup随意:

package zhangphil.layout;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class MyLayout extends ViewGroup {

    public MyLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // 度量全部子view要占用的空间,宽和高
    //onMeasure被Android系统调用是在onLayout之前
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //所有子view加起来总的Measured Dimension高度和宽度
        int measuredWidth = 0;
        int measuredHeight = 0;

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View v = getChildAt(i);
            if (v.getVisibility() != View.GONE) {
                measureChild(v, widthMeasureSpec, heightMeasureSpec);

                measuredWidth += v.getMeasuredWidth();

                //measuredDimensionHeight += v.getMeasuredHeight();
                measuredHeight=Math.max(measuredHeight, v.getMeasuredHeight());
            }
        }

        //仔细检查!不要疏忽掉一些padding的值
        measuredWidth += getPaddingLeft() + getPaddingRight();
        measuredHeight += getPaddingTop() + getPaddingBottom();

        //可选
        //measuredWidth = Math.max(measuredWidth, getSuggestedMinimumWidth());
        //measuredHeight = Math.max(measuredHeight, getSuggestedMinimumHeight());

        //另外一种set度量值的方法
        //setMeasuredDimension(resolveSize(measuredWidth, widthMeasureSpec),resolveSize(measuredHeight, heightMeasureSpec));

        setMeasuredDimension(measuredWidth, measuredHeight);
    }

    //Android系统在onMeasure之后调用onLayout
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //此时回调的参数l,t,r,b是上一步onMeasure计算出来的值。r是总宽度,b是总高度
        //我们在l,t,r,b这四个参数“框”出来的空间内一个一个摆放我们自己的子view

        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View v = getChildAt(i);
            if (v.getVisibility() != View.GONE) {
                int childWidth = v.getMeasuredWidth();
                int childHeight = v.getMeasuredHeight();

                //开始摆放
                v.layout(l, t, l + childWidth, t + childHeight);

                //把左边的锚定位置往右移
                l += childWidth;

                //本例简单演示的是水平摆放子view,所以此处不用累加高度
                //t += childHeight;
            }
        }
    }
}

写一个布局文件,这个布局不再使用Android已经为我们准备好的布局,而是MyLayout布局,MyLayout布局里面简单套几个TextView作为子布局:

<?xml version="1.0" encoding="utf-8"?>
<zhangphil.layout.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="100dip"
        android:layout_height="50dip"
        android:background="@android:color/holo_red_light"
        android:gravity="center"
        android:text="Zhang" />

    <TextView
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:background="@android:color/holo_blue_light"
        android:gravity="center"
        android:text="Phil" />

    <TextView
        android:layout_width="80dip"
        android:layout_height="50dip"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:text="\@CSDN" />
</zhangphil.layout.MyLayout>

如果不想写xml布局也可以,那么在setContentView里面new一个MyLayout()直接放进去也一样。

写一个MainActivity.java测试,其实就是把xml布局直接setContentView()里面,除此之外没有其他任何代码:

package zhangphil.layout;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

代码运行结果:

Android自定义ViewGroup:onMeasure与onLayout(1)

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
东方客主 东方客主
3年前
Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条
问题1、页面布局文件:<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:id"@id/layoutorderdetail"android:layoutwidth"matchparent"android:layoutheigh
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
九章 九章
2年前
Android-自定义view
要自定义view,都知道有3个方法需要重写:onMeasure、onLayout、onDraw。而且这三个方法的执行是按顺序的。生命周期image.png实际开发中,比较多的自定义都是具体实现一个view的子类,实现viewgroup的子类比较少,两者基本相似,区别就是view需要实现onMeasure、onLayout、onDraw三个方法,而vie
Stella981 Stella981
2年前
50 Android Hack 读书笔记
1、可以指定android:weightSum属性2、使用include标签来应对变化3、使用ViewStub标签延迟加载有可能不需要加载的数据标签中可以指定inflateId属性4、使用自定义ViewGroup,重写onMeasure、onLayout5、使用Android的PreferenceCategory6、使用TextSwitcher
Stella981 Stella981
2年前
Spinner使用
1.在xml文件设立布局文件<Spinner    android:layout\_width"wrap\_content"    android:layout\_height"wrap\_content"    android:layout\_below"@id/b1"    andro
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
可莉 可莉
2年前
2019年Android岗位BAT等大厂面试题,希望对新的一年的你有所帮助
2019年Android岗位BAT等大厂面试题知识点小结2019年了搜集了很多面试题,希望能对大家有所帮助1.View的绘制流程;自定义View如何考虑机型适配;自定义View的事件分发机制;View和ViewGroup分别有哪些事件分发相关的回调方法;自定义View如何提供获取View属
Stella981 Stella981
2年前
2019年Android岗位BAT等大厂面试题,希望对新的一年的你有所帮助
2019年Android岗位BAT等大厂面试题知识点小结2019年了搜集了很多面试题,希望能对大家有所帮助1.View的绘制流程;自定义View如何考虑机型适配;自定义View的事件分发机制;View和ViewGroup分别有哪些事件分发相关的回调方法;自定义View如何提供获取View属
Stella981 Stella981
2年前
Android选项卡TabHost功能和用法
1、布局文件<TabHostxmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:id"@android:id/tabhost"