目錄
目錄
效果展示
知識(shí)預(yù)備
●ViewGroup的繪制流程
ViewGroup的繪制流程分為三步即:onMeasure(),onLayout(),onDraw()。它們對(duì)應(yīng)的作用分別是:
onMeasure():測(cè)量當(dāng)前控件的大小
onLayout():負(fù)責(zé)子控件的布局
onDraw():繪制控件
●MeasureSpec數(shù)值提取
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
在onMeasure()方法中有兩個(gè)參數(shù)需要我們進(jìn)行處理,一個(gè)是widthMeasureSpec另一個(gè)是heightMeasureSpec,這兩個(gè)參數(shù)中的每一個(gè)參數(shù)都包含兩個(gè)數(shù)值,一個(gè)是模式一個(gè)是大小,其中模式是布局中為控件設(shè)置的寬高是match_parent還是wrap_content或是具體值,而大小就是寬高的具體數(shù)值了。在Android中模式一共有三種:
MeasureSpec.AT_MOST:元素至多達(dá)到的值,對(duì)應(yīng)wrap_content
MeasureSpec.EXACTLY:精確的值,對(duì)應(yīng)match_parent或者具體值(如10dp)
MeasureSpec.UNSPECIFIED:未知(一般用不到)
兩個(gè)數(shù)值的具體提取方式如下:
@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);
}
取出具體數(shù)值后再根據(jù)不同的模式設(shè)置控件的大小,使用的方法是setMeasuredDimension(int measuredWidth, int measuredHeight)傳入的是寬度和高度。
案例實(shí)現(xiàn)
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);//測(cè)量子控件
int childMeasuredWidth = child.getMeasuredWidth();//獲取子控件的寬度
int childMeasuredHeight = child.getMeasuredHeight();//獲取子控件的高度
height+=childMeasuredHeight;//將所有子控件的高度加起來(lái)
width += childMeasuredWidth;//將所有子控件的高度加起來(lái)
}
//根據(jù)模式判斷是否將所有子控件的高度和或?qū)挾群驮O(shè)置為當(dāng)前ViewGroup的高度或?qū)挾? 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();
//遍歷子控件并對(duì)子控件進(jìn)行布局
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);//對(duì)子控件進(jìn)行布局
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>