【自定义】ViewGroup初探

来源:互联网 发布:淘宝买多肉哪家店靠谱 编辑:程序博客网 时间:2024/05/17 09:07

一、简述

ViewGroup:视图容器,顾名思义用于盛放视图的。

Android的6大布局,都是自定义ViewGroup,因为都继承了ViewGroup。

此文,先不介绍自定义ViewGroup,先观察一下6大布局是怎么重写ViewGroup的。

打开LinearLayout,ctrl+o操作,看一下类里面的方法和结构,除了有很多方法,还包含LayoutParams类。

依次打开其他布局,发现都有这个LayoutParams:布局参数


二、ViewGroup中的LayoutParams(布局参数)

个人理解:存储子View的属性参数,描述自己多宽、多高等等,加入到ViewGroup。

布局参数,最基本的就是宽,高,当然也可以有其他参数,如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.wgl.share.share.MainActivity"></LinearLayout>


LinearLayout在Android的attrs文件里必定有一个声明属性的标签,进入attrs.xml

<declare-styleable name="LinearLayout_Layout">        <attr name="layout_width" />        <attr name="layout_height" />        <attr name="layout_weight" format="float" />        <attr name="layout_gravity" />    </declare-styleable>


RelativeLayout得到这些属性值,是靠TypeArray,进入RelativeLayout:

public class LinearLayout extends ViewGroup {    //略......    public static class LayoutParams extends ViewGroup.MarginLayoutParams {        public LayoutParams(Context c, AttributeSet attrs) {            super(c, attrs);            TypedArray a = c.obtainStyledAttributes(attrs,                    com.android.internal.R.styleable.RelativeLayout_Layout);    //略......  }    public LayoutParams(int w, int h) {            super(w, h);        }               public LayoutParams(ViewGroup.LayoutParams source) {            super(source);        }        public LayoutParams(ViewGroup.MarginLayoutParams source) {            super(source);        }  }}


观察上面代码,我发现这个方法LayoutParams(int w, int h),动态加载View时,设置宽高的,项目中很常见。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10,10);View view = new View(this);view.setLayoutParams(params);//设置图片view.setBackgroundResouce(R.mipmap.a)LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);linearLayout.addView(view);

注意:如果你给LinearLayout动态添加的View,就用LinearLayout.LayoutParams,根据布局而定。

想找一个完整的动态添加View,可以搜索:页面指示器。



三、自定义ViewGroup中的方法


1.onMeasure(...):View中的方法

测量方法,如果不重写,只会测量ViewGroup的宽和高,子View的宽高都为0。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    }
这样会引起什么呢?子View无法显示,具体请往下看。


2.onLayout(...):ViewGroup的方法,给子View安排位置,如下

protected void onLayout(boolean changed, int l, int t, int r, int b) {        int left = 0;int top = 0; int right = 0; int bottom = 0;        int count = getChildCount();        for(int i = 0; i < count; i++){            View child = getChildAt(i);            right = left + child.getMeasuredWidth();            bottom = top + child.getMeasuredHeight();            child.layout(left, top, right, bottom);//给子view定位        }    }
请注意child.getMeasuredWith()和child.getMeasuredHeight(),如果onMeasure(...)方法没有测量子View的宽高,那么子View的左边距、上边距、右边距、下边距,都是0,肯定看不见的。

怎么解决?回到onMeasure(...)方法,修改如下:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int count = getChildCount();//        int childMeasuredWidth = 0;//        int childMeasuredHeight = 0;//        int childWidthMeasureSpec = 0;//        int childHeightMeasureSpec = 0;        for(int i = 0; i < count; i++){            View child = getChildAt(i);            //选择一种测量就行            //系统推荐测量            measureChild(child, widthMeasureSpec, heightMeasureSpec);            //自己测量//             childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);//             childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);//             child.measure(childWidthMeasureSpec, childHeightMeasureSpec);        }    }


两种测量方式的区别是什么?

a.系统测量,会根据布局的宽高设置模式,测量子View。

布局宽高设置了明确值或者macth_parent或者fill_parent,对应MeasureSpec.EXACTLY模式

布局宽高设置wrap_content,对应MeasureSpec.AT_MOST模式

MeasureSpec.UNSPECIFIED模式(很少用)

b.自己测量,makeMeasureSpec(int size, int mode)返回一个尺寸和模式的合成值(这个合成值是32位的,int类型。前2位表示mode,后30位表示size。),然后再用measure(宽的合成值,高的合成值)测量子view大小。


3.根据以上两点介绍,我们总结出:只有重写onMeasure,才能得到子View宽高,才能正常显示。可见测量方法,至关重要,还有一点,onMeasure方法,不是开始的时候就执行,onCreate,onStart,onResume执行完毕之后,他才会执行。








0 0