Android 重写ViewGroup 分析onMeasure()和onLayout()方法

Android 重写ViewGroup 分析onMeasure()和onLayout()方法,第1张

概述Android重写ViewGroup分析onMeasure()和onLayout()方法在继承ViewGroup类时,需要重写两个方法,分别是onMeasure和onLayout。

AndroID 重写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)方法

voID androID.vIEw.VIEwGroup.onLayout(boolean changed,int l,int t,int r,int b)

调用场景:在vIEw给其孩子设置尺寸和位置时被调用。子vIEw,包括孩子在内,必须重写onLayout(boolean,int)方法,并且调用各自的layout(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:

package net.loonggg.vIEwgroup;  import androID.content.Context; import androID.util.AttributeSet; import androID.vIEw.VIEw; import androID.vIEw.VIEwGroup;  public class MyVIEwGroup extends VIEwGroup {    public MyVIEwGroup(Context context) {     super(context);   }    public MyVIEwGroup(Context context,AttributeSet attrs) {     super(context,attrs);   }    public MyVIEwGroup(Context context,AttributeSet attrs,int defStyle) {     super(context,attrs,defStyle);   }    /**    * 计算控件的大小    */   @OverrIDe   protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {     super.onMeasure(wIDthMeasureSpec,heightmeasureSpec);     int measureWIDth = measureWIDth(wIDthMeasureSpec);     int measureHeight = measureHeight(heightmeasureSpec);     // 计算自定义的VIEwGroup中所有子控件的大小     measureChildren(wIDthMeasureSpec,heightmeasureSpec);     // 设置自定义的控件MyVIEwGroup的大小     setMeasuredDimension(measureWIDth,measureHeight);   }    private int measureWIDth(int pWIDthMeasureSpec) {     int result = 0;     int wIDthMode = MeasureSpec.getMode(pWIDthMeasureSpec);// 得到模式     int wIDthSize = MeasureSpec.getSize(pWIDthMeasureSpec);// 得到尺寸      switch (wIDthMode) {     /**      * mode共有三种情况,取值分别为MeasureSpec.UnspecIFIED,MeasureSpec.EXACTLY,* MeasureSpec.AT_MOST。      *      *      * MeasureSpec.EXACTLY是精确尺寸,      * 当我们将控件的layout_wIDth或layout_height指定为具体数值时如andorID      * :layout_wIDth="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。      *      *      * MeasureSpec.AT_MOST是最大尺寸,      * 当控件的layout_wIDth或layout_height指定为WRAP_CONTENT时      * ,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可      * 。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。      *      *      * MeasureSpec.UnspecIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterVIEw,      * 通过measure方法传入的模式。      */     case MeasureSpec.AT_MOST:     case MeasureSpec.EXACTLY:       result = wIDthSize;       break;     }     return result;   }    private int measureHeight(int pHeightmeasureSpec) {     int result = 0;      int heightmode = MeasureSpec.getMode(pHeightmeasureSpec);     int heightSize = MeasureSpec.getSize(pHeightmeasureSpec);      switch (heightmode) {     case MeasureSpec.AT_MOST:     case MeasureSpec.EXACTLY:       result = heightSize;       break;     }     return result;   }    /**    * 覆写onLayout,其目的是为了指定视图的显示位置,方法执行的前后顺序是在onMeasure之后,因为视图肯定是只有知道大小的情况下,    * 才能确定怎么摆放    */   @OverrIDe   protected voID onLayout(boolean changed,int b) {     // 记录总高度     int mTotalHeight = 0;     // 遍历所有子视图     int childCount = getChildCount();     for (int i = 0; i < childCount; i++) {       VIEw childVIEw = getChildAt(i);        // 获取在onMeasure中计算的视图尺寸       int measureHeight = childVIEw.getMeasuredHeight();       int measureDWIDth = childVIEw.getMeasureDWIDth();        childVIEw.layout(l,mTotalHeight,measureDWIDth,mTotalHeight           + measureHeight);        mTotalHeight += measureHeight;      }   }  } 

第二步,布局文件:

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   xmlns:tools="http://schemas.androID.com/tools"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:background="#00f0f0"   tools:context=".MainActivity" >    <net.loonggg.vIEwgroup.MyVIEwGroup     androID:ID="@+ID/myVIEwGroup"     androID:layout_wIDth="480dp"     androID:layout_height="300dp"     androID:background="#0f0f0f" >      <TextVIEw       androID:layout_wIDth="200dp"       androID:layout_height="100dp"       androID:background="#000000"       androID:gravity="center"       androID:text="第一个TextVIEw" />      <TextVIEw       androID:layout_wIDth="100dp"       androID:layout_height="200dp"       androID:background="#ffffff"       androID:gravity="center"       androID:text="第二个TextVIEw" />   </net.loonggg.vIEwgroup.MyVIEwGroup>  </relativeLayout> 

第三步,MainActivity.java:

package net.loonggg.vIEwgroup;  import androID.os.Bundle; import androID.app.Activity;  public class MainActivity extends Activity {    @OverrIDe   protected voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);   }  } 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android 重写ViewGroup 分析onMeasure()和onLayout()方法全部内容,希望文章能够帮你解决Android 重写ViewGroup 分析onMeasure()和onLayout()方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1146120.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存