Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解

MainActivity如下:

package cc.testviewstudy5;

import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 自定义ViewGroup及其onMeasure()的理解
 *
 * 参考资料:
 * 1 http://blog.csdn.net/guolin_blog/article/details/16330267
 * 2 http://blog.csdn.net/dawanganban/article/details/23953827
 *   Thank you very much
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new ViewGroupSubClass(this));
	}

}

ViewGroupSubClass如下:

package cc.testviewstudy5;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * 当继承自ViewGroup时,如果在onMeasure()对于子View不调用
 * 1 measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec)
 * 2 childView.measure(childWidthMeasureSpec, childHeightMeasureSpec)
 * 这两个方法的任何其中一个,那么:
 * childView.getMeasuredWidth()和childView.getMeasuredHeight()得到的值均为0
 *
 * 这是为什么呢?
 * 因为ViewGroup继承自View,View就根本没有子View.所以:
 * 在ViewGroup的onMeasure()中的super.onMeasure(widthMeasureSpec,heightMeasureSpec)
 * 只是测量该ViewGroup本身的宽和高.而没有去测量其每个子View的宽和高.
 * 于是需要我们自己写代码去测量该ViewGroup的每个子View,或者让子View自己调用measure().这么操作以后,再用
 * childView.getMeasuredWidth()和childView.getMeasuredHeight()
 * 得到的值就不再是0了.
 *
 * 假若不继承自ViewGroup而继承自XXXLayout,那么就不是必须要自己去测量每个子View的大小了.
 * 查看XXXLayout的源代码,可以看到在其onMeasure()中已经测量了子View的大小.
 *
 */
public class ViewGroupSubClass extends ViewGroup {

	public ViewGroupSubClass(Context context) {
		super(context);
		Button button1 = new Button(context);
		button1.setText("button1");
		this.addView(button1);

		Button button2 = new Button(context);
		button2.setText("button2");
		this.addView(button2);

		Button button3 = new Button(context);
		button3.setText("button3");
		this.addView(button3);

	}

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

	public ViewGroupSubClass(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 获取系统自动测量的该ViewGroup的大小
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		System.out.println("获取系统自动测量的该ViewGroup的大小: widthSize="+widthSize+",heightSize="+heightSize);
		// 我们也可调用setMeasuredDimension()重新设置测量结果

		// 修改了系统自动测量的子View的大小
		int childCount = this.getChildCount();
		int childMeasuredWidth = 0;
		int childMeasuredHeight = 0;
		int childWidthMeasureSpec = 0;
		int childHeightMeasureSpec = 0;
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);

			// 系统自动测量子View:
			measureChild(childView, widthMeasureSpec, heightMeasureSpec);

			// 如果不希望系统自动测量子View,我们用以下的方式:
			// childWidthMeasureSpec =
			// MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
			// childHeightMeasureSpec =
			// MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
			// childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
		}

		// 获取每个子View测量所得的宽和高
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);
			childMeasuredWidth = childView.getMeasuredWidth();
			childMeasuredHeight = childView.getMeasuredHeight();
			System.out.println("i=" + i+ ",获取系统自动测量的该子View的大小:" +
					          "childMeasuredWidth="+ childMeasuredWidth + "," +
					          "childMeasuredHeight="+ childMeasuredHeight);
		}
	}

	@Override
	protected void onLayout(boolean arg0, int l, int t, int r, int b) {
		System.out.println("该ViewGroup的布局:"+"l="+l+",t="+t+",r="+r+",b="+b);
		int childCount = getChildCount();
		int left = 0;
		int top = 10;
		int right = 0;
		int bottom = 0;
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);
			right = left + childView.getMeasuredWidth();
			// 也可不用测量所得的宽而指定一个值
			// right=left+180;
			bottom = top + childView.getMeasuredHeight();
			// 也可不用测量所得的高而指定一个值
			// bottom=top+180;
			childView.layout(left, top, right, bottom);
			System.out.println("i=" + i + ",该子View的布局:" + "" +
					          "left="+left+",top="+top+",right="+right+",bottom="+bottom);
			top += 190;
		}

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <cc.testviewstudy5.ButtonSubClass
        android:id="@+id/button"
        android:layout_width="100dip"
        android:layout_height="200dip"
        android:text="Button"/>

</RelativeLayout>

PS:
main.mxl没有任何用处

时间: 2024-08-30 10:09:40

Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解的相关文章

android自定义view和自定义layout

问题描述 android自定义view和自定义layout 我自定义了一个view,实现一个图片的移动,然后我想在view添加一个按钮,网上说是要建一个layout,然后再add.求教具体方法啊,能不能不用layout直接用view添加按钮. 解决方案 类似如下 LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); Lin

Android图表库MPAndroidChart(五)——自定义MarkerView实现选中高亮

Android图表库MPAndroidChart(五)--自定义MarkerView实现选中高亮 在学习本课程之前我建议先把我之前的博客看完,这样对整体的流程有一个大致的了解 Android图表库MPAndroidChart(一)--了解他的本质,方能得心应手 Android图表库MPAndroidChart(二)--线形图的方方面面,看完你会回来感谢我的 Android图表库MPAndroidChart(三)--双重轴线形图的实现,这次就so easy了 Android图表库MPAndroid

Android自定义View之自定义评价打分控件RatingBar实现自定义星星大小和间距_Android

在Android开发中,我们经常会用到对商家或者商品的评价,运用星星进行打分.然而在Android系统中自带的打分控件,RatingBar特别不好用,间距和大小无法改变.所以,我就自定义了一个特别好用的打分控件.在项目中可以直接使用,特别简单.下面直接上图: 效果图 实现原理 其实就是自定义View继承LinearLayout ,然后里面动态加了五个ImageView. 实现代码,有详细的注释 在attrs中声明的可以在xml中设置的变量 <declare-styleable name="

Android学习笔记(五)

1. DatePicker和DatePickerDialog的用法 DatePicker是用于选择日期的控件,和TimePicker类似,一般在设置系统日期和时间的时候可以看到.        DatePicker和以前的控件的一个很大的区别在于,这个控件不在布局文件中声明.        1.声明一个监听器,使用匿名内部类: ? 1 2 3 4 5 6 DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDi

自定义滑动按钮为例图文剖析Android自定义View绘制_Android

自定义View一直是横在Android开发者面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看,ViewGroup继承View. View的子类,多是功能型的控件,提供绘制的样式,比如imageView,TextView等,而ViewGroup的子类,多用于管理控件的大小,位置,如LinearLayout,RelativeLayout等,从下图可以看出 从实际应用中看,他们又是组合关系,我们在布局中,常常是一个ViewGroup嵌套多个ViewGro

自定义滑动按钮为例图文剖析Android自定义View绘制

自定义View一直是横在Android开发者面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看,ViewGroup继承View. View的子类,多是功能型的控件,提供绘制的样式,比如imageView,TextView等,而ViewGroup的子类,多用于管理控件的大小,位置,如LinearLayout,RelativeLayout等,从下图可以看出 从实际应用中看,他们又是组合关系,我们在布局中,常常是一个ViewGroup嵌套多个ViewGro

Android 自定义View是继承view还是viewgroup

问题描述 Android 自定义View是继承view还是viewgroup 我一直对于自定义View一知半解,不知道该怎么去 入手,看到一些自定义的view,一些继承的view,一些是ViewGroup,还有的就是一些自带的控件,怎么去区分我所要的view该继承谁?求郭老师指导一下! 解决方案 看你的具体需求,如果单独一个控件就继承View,如果还要承装其他控件就继承ViewGroup. 解决方案二: Android 自定义View和ViewGroupAndroid 自定义View 和 Vie

Android自定义view实现阻尼效果的加载动画_Android

效果: 需要知识: 1. 二次贝塞尔曲线 2. 动画知识 3. 基础自定义view知识 先来解释下什么叫阻尼运动 阻尼振动是指,由于振动系统受到摩擦和介质阻力或其他能耗而使振幅随时间逐渐衰减的振动,又称减幅振动.衰减振动.[1] 不论是弹簧振子还是单摆由于外界的摩擦和介质阻力总是存在,在振动过程中要不断克服外界阻力做功,消耗能量,振幅就会逐渐减小,经过一段时间,振动就会完全停下来.这种振幅随时间减小的振动称为阻尼振动.因为振幅与振动的能量有关,阻尼振动也就是能量不断减少的振动.阻尼振动是非简谐运

Android自定义View实现验证码_Android

本文章是基于鸿洋的Android 自定义View (一) 的一些扩展,以及对Android自定义View构造函数详解里面内容的一些转载. 首先我们定义一个declare-styleable标签declare-styleable标签的作用是给自定义控件添加自定义属性用的例如这样 (我们定义了文字的颜色,大小,长度,跟背景的颜色) <declare-styleable name="CustomTitleView"> <attr name="titleColor&