目录
效果展示
知识预备
●ViewGroup的绘制流程
ViewGroup的绘制流程分为三步即:onMeasure(),onLayout(),onDraw()。它们对应的作用分别是:
onMeasure():测量当前控件的大小
onLayout():负责子控件的布局
onDraw():绘制控件
●MeasureSpec数值提取
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
在onMeasure()方法中有两个参数需要我们进行处理,一个是widthMeasureSpec另一个是heightMeasureSpec,这两个参数中的每一个参数都包含两个数值,一个是模式一个是大小,其中模式是布局中为控件设置的宽高是match_parent还是wrap_content或是具体值,而大小就是宽高的具体数值了。在Android中模式一共有三种:
MeasureSpec.AT_MOST:元素至多达到的值,对应wrap_content
MeasureSpec.EXACTLY:精确的值,对应match_parent或者具体值(如10dp)
MeasureSpec.UNSPECIFIED:未知(一般用不到)
两个数值的具体提取方式如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
}
取出具体数值后再根据不同的模式设置控件的大小,使用的方法是setMeasuredDimension(int measuredWidth, int measuredHeight)传入的是宽度和高度。
案例实现
public class MyLinearLayout extends ViewGroup {
public MyLinearLayout(Context context) {
this(context,null);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
int width = 0;
int height = 0;
int childCount = getChildCount();
//遍历子控件
for (int i = 0;i< childCount;i++){
View child = getChildAt(i);
measureChild(child,widthMeasureSpec,heightMeasureSpec);//测量子控件
int childMeasuredWidth = child.getMeasuredWidth();//获取子控件的宽度
int childMeasuredHeight = child.getMeasuredHeight();//获取子控件的高度
height+=childMeasuredHeight;//将所有子控件的高度加起来
width += childMeasuredWidth;//将所有子控件的高度加起来
}
//根据模式判断是否将所有子控件的高度和或宽度和设置为当前ViewGroup的高度或宽度
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY?measureWidth:width),(heightMode == MeasureSpec.EXACTLY?measureHeight:height));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int top = 0;
int left = 0;
int childCount = getChildCount();
//遍历子控件并对子控件进行布局
for(int i = 0 ; i< childCount ;i++){
View child = getChildAt(i);
int measuredWidth = child.getMeasuredWidth();
int measuredHeight = child.getMeasuredHeight();
child.layout(left,top,left+measuredWidth,top+measuredHeight);//对子控件进行布局
top+=measuredHeight;
left+=measuredWidth;
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<com.itfitness.module.customdemo.viewgroup.MyLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="我是一"
android:background="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是二"
android:background="#ff0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是三"
android:background="#0f0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是四"
android:background="#00f"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是五"
android:background="#cf0"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="我是六"
android:background="#fcc"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.itfitness.module.customdemo.viewgroup.MyLinearLayout>