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

整理总结自鸿洋的博客: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>

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
viewgroup
自定义viewgroup、安卓自定义viewgroup、自定义滑动viewgroup、自定义viewgroup 刷新、自定义viewgroup实现,以便于您获取更多的相关知识。

时间: 2024-09-18 21:22:21

Android自定义ViewGroup之第一次接触ViewGroup_Android的相关文章

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 { @Overr

viewgroup 传参-android自定义ViewGroup的问题

问题描述 android自定义ViewGroup的问题 在名为Demo的activity中用到了一个继承ViewGroup的类MyView来布局,具体是这样的 在Demo的布局文件xml中, 在Demo的代码中 MyView scroll = (MyView) findViewById(R.id.view1); 在MyView中的构造函数 public MyView(Context context, AttributeSet attrs) { //各类操作 } 问题是Demo需要给scroll传

Android自定义ViewGroup打造各种风格的SlidingMenu_Android

上篇给大家介绍QQ5.0侧滑菜单的视频课程,对于侧滑的时的动画效果的实现有了新的认识,似乎打通了任督二脉,目前可以实现任意效果的侧滑菜单了,感谢鸿洋大大!! 用的是HorizontalScrollView来实现的侧滑菜单功能,HorizontalScrollView的好处是为我们解决了滑动功能,处理了滑动冲突问题,让我们使用起来非常方便,但是滑动和冲突处理都是android中的难点,是我们应该掌握的知识点,掌握了这些,我们可以不依赖于系统的API,随心所欲打造我们想要的效果,因此这篇文章我将直接

Android自定义ViewGroup实现绚丽的仿支付宝咻一咻雷达脉冲效果_Android

去年春节的时候支付宝推行的集福娃活动着实火的不能再火了,更给力的是春晚又可以全民参与咻一咻集福娃活动,集齐五福就可平分亿元大红包,只可惜没有敬业福--那时候在家没事写了个咻一咻插件,只要到了咻一咻的时间点插件就可以自动的点击咻一咻来咻红包,当时只是纯粹练习这部分技术代码没有公开,后续计划写篇关于插件这方面的文章,扯远了(*^__^*) --我们知道在支付宝的咻一咻页面有个雷达扩散的动画效果,当时感觉动画效果非常棒,于是私下尝试着实现了类似的效果,后来在github发现有大神也写有类似效果,于是读

Android自定义ViewGroup实现标签流容器FlowLayout_Android

本篇文章讲的是Android 自定义ViewGroup之实现标签流式布局-FlowLayout,开发中我们会经常需要实现类似于热门标签等自动换行的流式布局的功能,网上也有很多这样的FlowLayout,但不影响我对其的学习.和往常一样,主要还是想总结一下自定义ViewGroup的开发过程以及一些需要注意的地方. 按照惯例,我们先来看看效果图 一.写代码之前,有几个是问题是我们先要弄清楚的: 1.什么是ViewGroup:从名字上来看,它可以被翻译为控件组,言外之意是ViewGroup内部包含了许

Android自定义ViewGroup打造各种风格的SlidingMenu

上篇给大家介绍QQ5.0侧滑菜单的视频课程,对于侧滑的时的动画效果的实现有了新的认识,似乎打通了任督二脉,目前可以实现任意效果的侧滑菜单了,感谢鸿洋大大!! 用的是HorizontalScrollView来实现的侧滑菜单功能,HorizontalScrollView的好处是为我们解决了滑动功能,处理了滑动冲突问题,让我们使用起来非常方便,但是滑动和冲突处理都是android中的难点,是我们应该掌握的知识点,掌握了这些,我们可以不依赖于系统的API,随心所欲打造我们想要的效果,因此这篇文章我将直接

Android自定义ViewGroup的实现方法_Android

     在android中提供了常见的几种ViewGroup的实现,包括LinearLayout.Relativeayout.FrameLayout等.这些ViewGroup可以满足我们一般的开发需求,但是对于界面要求复杂的,这几个布局就显得捉襟见肘了.所以自定义的ViewGroup在我们接触过的应用中比比皆是.      要想实现一个自定义的ViewGroup,第一步是学会自定义属性,这些自定义的属性将让我们配置布局文件的时候更加的灵活.自定义属性是在value目录下声明一个attrs.xm

Android自定义ViewGroup实现受边界限制的滚动操作(3)_Android

上一篇文章<自定义viewgroup(2)>地址:http://www.jb51.net/article/100610.htm 代码 package com.example.libingyuan.horizontallistview.ScrollViewGroup; import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import androi

Android自定义ViewGroup之子控件的自动换行和添加删除

常用的布局类型并不能满足所有需求,这时就会用到ViewGroup. ViewGroup作为一个放置View的容器,并且我们在写布局xml的时候,会告诉容器(凡是以layout为开头的属性,都是为用于告诉容器的),我们的宽度(layout_width).高度(layout_height).对齐方式(layout_gravity)等:当然还有margin等:于是乎,ViewGroup需要做的事情是:给childView计算出建议的宽和高和测量模式 :决定childView的位置:为什么只是建议的宽和