Android自定义ViewGroup之第一次接触ViewGroup

 更新时间:2016年06月16日 09:11:13   作者:可乐淘  
这篇文章主要为大家详细介绍了Android自定义ViewGroup之第一次接触ViewGroup,感兴趣的小伙伴们可以参考一下

整理总结自鸿洋的博客:http://blog.csdn.net/lmj623565791/article/details/38339817/
 一、com.cctvjiatao.customviewgroup.act.MainActivity.Java
 需求:我们定义一个ViewGroup,内部可以传入0到4个childView,分别依次显示在左上角,右上角,左下角,右下角

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
//  setContentView(R.layout.activity_main2);
//  setContentView(R.layout.activity_main3);
 }
}

二、com.cctvjiatao.customviewgroup.view.CustomViewGroup.java
 一)、ViewGroup是是什么?作用呢?
1、它相当于放置View的容器。xml布局文件中,凡是以“layout”开头的属性都是和ViewGroup(即容器)相关的,比如高度(layout_height)、宽度(layout_width)、对齐方式(layout_gravity)等;
2、它给childView计算出建议的宽、高和测量模式,决定childView的位置;
为什么是“建议的宽、高”而不是直接确定呢?因为当childView的宽、高设置为wrap_content时,只有childView自己才能计算出自己的宽和高。
 二)、View的作用是什么?
1、它根据测量模式和ViewGroup给出的建议的宽、高,计算出自己的宽、高;
2、在ViewGroup为其指定的区域内绘制自己的形态;
三)、View的三种测量模式
1、EXACTLY:表示设置了精确的值,一般当childView设置其宽高为精确值、match_parent时,ViewGroup会将其设置为EXACTLY;
2、AT_MOST:表示子布局被限制在一个最大值内,一般当childView设置其宽、高为wrap_content时,ViewGroup会将其设置为AT_MOST;
3、UNSPECIFIED:表示子布局想要多大就多大,一般出现在AadapterView的item的heightMode中、ScrollView的childView的heightMode中;此种模式比较少见。
四)、ViewGroup 和 LayoutParams的关系
当在LinearLayout中写childView的时候,可以写layout_gravity,layout_weight属性;
而在RelativeLayout中的childView有layout_centerInParent属性,却没有layout_gravity,layout_weight,这是为什么呢?
这是因为每个ViewGroup需要指定一个LayoutParams,用于确定支持childView支持哪些属性,比如LinearLayout指定LinearLayout.LayoutParams等
如果去看LinearLayout的源码,会发现其内部定义了LinearLayout.LayoutParams,在此类中,你可以发现weight和gravity的身影。
五)、从API角度分析ViewGroup和View的作用
View 根据 ViewGroup 传入的测量值和测量模式,确定自己的宽、高(在onMeasure中完成),然后在onDraw中完成对自己的绘制;
ViewGroup需要给View传入View的测量值和测量模式(在onMeasure中完成),而且对于此ViewGroup的父布局,ViewGroup也要在onMeasure中完成对自己宽、高的确定;
ViewGroup需要再onLayout中完成对其childView的位置的指定。

public class CustomViewGroup extends ViewGroup {

 public CustomViewGroup(Context context) {
  super(context);
 }

 public CustomViewGroup(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 /**
  * 一、重写generateLayoutParams,确定该ViewGroup的LayoutParams
  * 返回MarginLayoutParams的实例,这样就为我们的ViewGroup指定了其LayoutParams为MarginLayoutParams
  */
 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new MarginLayoutParams(getContext(), attrs);
 }

 /**
  * 二、计算所有ChildView的宽度和高度 然后根据ChildView的计算结果,设置自己的宽和高
  */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  //1、获得此ViewGroup上级容器为其推荐的宽和高,以及计算模式
  int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
  // 2、计算出所有的childView的宽和高
  measureChildren(widthMeasureSpec, heightMeasureSpec);
  // 3、如果ViewGroup布局是wrap_content时,根据childView的尺寸,计算容器的宽和高
  int width = 0;//ViewGroup的宽度
  int height = 0;//ViewGroup的高度
  int cCount = getChildCount();//childView的数量
  int cWidth = 0;//childView的总宽度
  int cHeight = 0;//childView的总高度
  MarginLayoutParams cParams = null;//View的测量模式
  int lHeight = 0;// 用于计算左边两个childView的高度
  int rHeight = 0;// 用于计算右边两个childView的高度,最终高度取二者之间大值
  int tWidth = 0;// 用于计算上边两个childView的宽度
  int bWidth = 0;// 用于计算下面两个childiew的宽度,最终宽度取二者之间大值
  for (int i = 0; i < cCount; i++) {
   View childView = getChildAt(i);
   cWidth = childView.getMeasuredWidth();
   cHeight = childView.getMeasuredHeight();
   cParams = (MarginLayoutParams) childView.getLayoutParams();
   if (i == 0 || i == 1) {// 上面两个childView
    tWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
   }
   if (i == 2 || i == 3) {// 下面两个childView
    bWidth += cWidth + cParams.leftMargin + cParams.rightMargin;
   }
   if (i == 0 || i == 2) {// 左面两个childView
    lHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
   }
   if (i == 1 || i == 3) {// 右面两个childView
    rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;
   }
  }
  width = Math.max(tWidth, bWidth);//取最大宽度
  height = Math.max(lHeight, rHeight);//去最大高度
  //4、如果是wrap_content设置为我们计算的值;否则直接设置为父容器计算的值
  setMeasuredDimension(
    (widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width,
    (heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height
  );
 }

 /**
  * 三、重写onLayout,对其所有childView进行定位(设置childView的绘制区域)
  */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int cCount = getChildCount();
  int cWidth = 0;
  int cHeight = 0;
  MarginLayoutParams cParams = null;
  //遍历所有childView根据其宽和高,以及margin进行布局
  for (int i = 0; i < cCount; i++) {
   View childView = getChildAt(i);
   cWidth = childView.getMeasuredWidth();
   cHeight = childView.getMeasuredHeight();
   cParams = (MarginLayoutParams) childView.getLayoutParams();
   int cl = 0, ct = 0, cr = 0, cb = 0;
   switch (i) {
    case 0:
     cl = cParams.leftMargin;
     ct = cParams.topMargin;
     break;
    case 1:
     cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin;
     ct = cParams.topMargin;
     break;
    case 2:
     cl = cParams.leftMargin;
     ct = getHeight() - cHeight - cParams.bottomMargin;
     break;
    case 3:
     cl = getWidth() - cWidth - cParams.leftMargin - cParams.rightMargin;
     ct = getHeight() - cHeight - cParams.bottomMargin;
     break;
   }
   cr = cl + cWidth;
   cb = cHeight + ct;
   childView.layout(cl, ct, cr, cb);
  }
 }
}

三、三种布局
 activity_main.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="200dp"
 android:layout_height="200dp"
 android:background="#AA333333">

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#FF4444"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

activity_main2.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="#AA333333">

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#E5ED05"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

activity_main3.xml

<com.cctvjiatao.customviewgroup.view.CustomViewGroup 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="#AA333333">

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#E5ED05"
  android:gravity="center"
  android:text="0"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#00ff00"
  android:gravity="center"
  android:text="1"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="50dp"
  android:layout_height="50dp"
  android:background="#ff0000"
  android:gravity="center"
  android:text="2"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

 <TextView
  android:layout_width="150dp"
  android:layout_height="150dp"
  android:background="#0000ff"
  android:gravity="center"
  android:text="3"
  android:textColor="#FFFFFF"
  android:textSize="22sp"
  android:textStyle="bold" />

</com.cctvjiatao.customviewgroup.view.CustomViewGroup>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Flutter实现文本滚动高亮效果的示例讲解

    Flutter实现文本滚动高亮效果的示例讲解

    这篇文章主要介绍了如何利用Flutter时时渲染页面从而达到文本滚动高亮的效果,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-03-03
  • Android StickListView实现悬停效果

    Android StickListView实现悬停效果

    这篇文章主要介绍了Android StickListView实现悬停效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android中Viewpager禁止滑动的实现

    Android中Viewpager禁止滑动的实现

    有时候在开发中会遇到一些特别的要求,如在ViewPager中嵌入ListView,或者再嵌入一个ViewPager,那么在滑动的时候就会造成被嵌入的XXView不能滑动了,那么就把最外层的ViewPager禁止滑动吧,本文就介绍了Android中Viewpager禁止滑动的实现方法,需要的朋友可以参考。
    2017-05-05
  • Android使用Notification实现通知功能

    Android使用Notification实现通知功能

    这篇文章主要为大家详细介绍了Android使用Notification实现通知功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Android 自绘控件

    Android 自绘控件

    Android中自定义控件分为三种:1.自绘控件 2.组合控件 3.继承控件。本篇介绍下自绘控件。下面跟着小编一起来看下吧
    2017-02-02
  • OpenGL ES透视投影实现方法(四)

    OpenGL ES透视投影实现方法(四)

    这篇文章主要为大家详细介绍了OpenGL ES透视投影的实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • android 布局之ConstraintLayout的使用

    android 布局之ConstraintLayout的使用

    这篇文章主要介绍了android 布局之ConstraintLayout的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • Android 列表形式的切换的示例代码

    Android 列表形式的切换的示例代码

    本篇文章主要介绍了Android 列表形式的切换的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 利用kotlin实现一个饼图实例代码

    利用kotlin实现一个饼图实例代码

    饼状图是以不同颜色的圆的切片表示的值。下面这篇文章主要给大家介绍了关于利用kotlin实现一个饼图的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
    2017-12-12
  • Android自定义带有圆形进度条的可长按控件功能

    Android自定义带有圆形进度条的可长按控件功能

    这篇文章主要介绍了Android自定义带有圆形进度条的可长按控件,思路很简单,使用简单的画笔工具就可以完成这个控件,本文结合示例代码给大家介绍的非常详细,需要的朋友参考下吧
    2022-06-06

最新评论