通过重写ViewGroup学习onMeasure()和onLayout()方法

在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout。
1,在方法onMeasure中调用setMeasuredDimension方法
void android.view.View.setMeasuredDimension(int measuredWidth, int measuredHeight)
在onMeasure(int, int)中,必须调用setMeasuredDimension(int width, int height)来存储测量得到的宽度和高度值,如果没有这么去做会触发异常IllegalStateException。
2,在方法onMeasure中调用孩子的measure方法
void android.view.View.measure(int widthMeasureSpec, int heightMeasureSpec)
这个方法用来测量出view的大小。父view使用width参数和height参数来提供constraint信息。实际上,view的测量工作在onMeasure(int, int)方法中完成。因此,只有onMeasure(int, int)方法可以且必须被重写。参数widthMeasureSpec提供view的水平空间的规格说明,参数heightMeasureSpec提供view的垂直空间的规格说明。
3,解析onMeasure(int, int)方法
void android.view.View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)
测量view及其内容来确定view的宽度和高度。这个方法在measure(int, int)中被调用,必须被重写来精确和有效的测量view的内容。
在重写这个方法时,必须调用setMeasuredDimension(int, int)来存储测量得到的宽度和高度值。执行失败会触发一个IllegalStateException异常。调用父view的onMeasure(int, int)是合法有效的用法。
view的基本测量数据默认取其背景尺寸,除非允许更大的尺寸。子view必须重写onMeasure(int, int)来提供其内容更加准确的测量数值。如果被重写,子类确保测量的height和width至少是view的最小高度和宽度(通过getSuggestedMinimumHeight()和getSuggestedMinimumWidth()获取)。
4,解析onLayout(boolean, int, int, int, int)方法
void android.view.ViewGroup.onLayout(boolean changed, int l, int t, int r, int b)
调用场景:在view给其孩子设置尺寸和位置时被调用。子view,包括孩子在内,必须重写onLayout(boolean, int, int, int, int)方法,并且调用各自的layout(int, int, int, int)方法。
参数说明:参数changed表示view有新的尺寸或位置;参数l表示相对于父view的Left位置;参数t表示相对于父view的Top位置;参数r表示相对于父view的Right位置;参数b表示相对于父view的Bottom位置。.
5,解析View.MeasureSpec类
android.view.View.MeasureSpec
MeasureSpec对象,封装了layout规格说明,并且从父view传递给子view。每个MeasureSpec对象代表了width或height的规格。
MeasureSpec对象包含一个size和一个mode,其中mode可以取以下三个数值之一:
    UNSPECIFIED,1073741824 [0x40000000],未加规定的,表示没有给子view添加任何规定。
    EXACTLY,0 [0x0],精确的,表示父view为子view确定精确的尺寸。

    AT_MOST,-2147483648 [0x80000000],子view可以在指定的尺寸内尽量大。

在这里给大家举一个例子demo:

第一步:自定义一个View实现ViewGroup接口,即自定义ViewGroup:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package net.loonggg.viewgroup;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.View;  
  6. import android.view.ViewGroup;  
  7.   
  8. public class MyViewGroup extends ViewGroup {
  9.     public MyViewGroup(Context context) {
  10.         super(context);  
  11.     }  
  12.   
  13.     public MyViewGroup(Context context, AttributeSet attrs) {
  14.         super(context, attrs);  
  15.     }  
  16.   
  17.     public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
  18.         super(context, attrs, defStyle);  
  19.     }  
  20.   
  21.     /** 
  22.      * 计算控件的大小 
  23.      */  
  24.     @Override  
  25.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  26.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  27.         int measureWidth = measureWidth(widthMeasureSpec);  
  28.         int measureHeight = measureHeight(heightMeasureSpec);  
  29.         // 计算自定义的ViewGroup中所有子控件的大小  
  30.         measureChildren(widthMeasureSpec, heightMeasureSpec);  
  31.         // 设置自定义的控件MyViewGroup的大小  
  32.         setMeasuredDimension(measureWidth, measureHeight);  
  33.     }  
  34.   
  35.     private int measureWidth(int pWidthMeasureSpec) {  
  36.         int result = 0;  
  37.         int widthMode = MeasureSpec.getMode(pWidthMeasureSpec);// 得到模式  
  38.         int widthSize = MeasureSpec.getSize(pWidthMeasureSpec);// 得到尺寸  
  39.   
  40.         switch (widthMode) {  
  41.         /** 
  42.          * mode共有三种情况,取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, 
  43.          * MeasureSpec.AT_MOST。 
  44.          *  
  45.          *  
  46.          * MeasureSpec.EXACTLY是精确尺寸, 
  47.          * 当我们将控件的layout_width或layout_height指定为具体数值时如andorid 
  48.          * :layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。 
  49.          *  
  50.          *  
  51.          * MeasureSpec.AT_MOST是最大尺寸, 
  52.          * 当控件的layout_width或layout_height指定为WRAP_CONTENT时 
  53.          * ,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可 
  54.          * 。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。 
  55.          *  
  56.          *  
  57.          * MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView, 
  58.          * 通过measure方法传入的模式。 
  59.          */  
  60.         case MeasureSpec.AT_MOST:  
  61.         case MeasureSpec.EXACTLY:  
  62.             result = widthSize;  
  63.             break;  
  64.         }  
  65.         return result;  
  66.     }  
  67.   
  68.     private int measureHeight(int pHeightMeasureSpec) {
  69.         int result = 0;  
  70.   
  71.         int heightMode = MeasureSpec.getMode(pHeightMeasureSpec);
  72.         int heightSize = MeasureSpec.getSize(pHeightMeasureSpec);
  73.   
  74.         switch (heightMode) {  
  75.         case MeasureSpec.AT_MOST:  
  76.         case MeasureSpec.EXACTLY:  
  77.             result = heightSize;  
  78.             break;  
  79.         }  
  80.         return result;  
  81.     }  
  82.   
  83.     /** 
  84.      * 覆写onLayout,其目的是为了指定视图的显示位置,方法执行的前后顺序是在onMeasure之后,因为视图肯定是只有知道大小的情况下,
  85.      * 才能确定怎么摆放
  86.      */
  87.     @Override
  88.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  89.         // 记录总高度
  90.         int mTotalHeight = 0;
  91.         // 遍历所有子视图
  92.         int childCount = getChildCount();
  93.         for (int i = 0; i < childCount; i++) {
  94.             View childView = getChildAt(i);
  95.   
  96.             // 获取在onMeasure中计算的视图尺寸
  97.             int measureHeight = childView.getMeasuredHeight();
  98.             int measuredWidth = childView.getMeasuredWidth();
  99.             
  100.             childView.layout(l, mTotalHeight, measuredWidth, mTotalHeight + measureHeight);
  101.             
  102.             mTotalHeight += measureHeight;
  103.         }
  104.     }
  105. }
第二步,布局文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#00f0f0"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <net.loonggg.viewgroup.MyViewGroup  
  9.         android:id="@+id/myViewGroup"  
  10.         android:layout_width="480dp"  
  11.         android:layout_height="300dp"  
  12.         android:background="#0f0f0f" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="200dp"  
  16.             android:layout_height="100dp"  
  17.             android:background="#000000"  
  18.             android:gravity="center"  
  19.             android:text="第一个TextView" />  
  20.   
  21.         <TextView  
  22.             android:layout_width="100dp"  
  23.             android:layout_height="200dp"  
  24.             android:background="#ffffff"  
  25.             android:gravity="center"  
  26.             android:text="第二个TextView" />  
  27.     </net.loonggg.viewgroup.MyViewGroup>  
  28.   
  29. </RelativeLayout>  

第三步,MainActivity.java:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package net.loonggg.viewgroup;

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

  4. public class MainActivity extends Activity {
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState) {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main);
  9.     }
  10. }

ok,你们大家懂了吗?有问题请留言。

转自:http://blog.youkuaiyun.com/loongggdroid/article/details/17515113

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值