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

所属分类: 软件编程 / Android 阅读数: 1970
收藏 0 赞 0 分享

整理总结自鸿洋的博客: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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多