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

简介: MainActivity如下:package cc.testviewstudy5;import android.os.Bundle;import android.
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没有任何用处
相关文章
|
17天前
|
Java API 调度
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
Android系统 自定义开机广播,禁止后台服务,运行手动安装应用接收开机广播
40 0
|
17天前
|
存储 Java Linux
Android Mstar增加IR 自定义遥控头码完整调试过程
Android Mstar增加IR 自定义遥控头码完整调试过程
27 1
|
1月前
|
缓存 测试技术 Android开发
深入探究Android中的自定义View绘制优化策略
【4月更文挑战第8天】 在Android开发实践中,自定义View的绘制性能至关重要,尤其是当涉及到复杂图形和动画时。本文将探讨几种提高自定义View绘制效率的策略,包括合理使用硬件加速、减少不必要的绘制区域以及利用缓存机制等。这些方法不仅能改善用户体验,还能提升应用的整体性能表现。通过实例分析和性能测试结果,我们将展示如何有效地实现这些优化措施,并为开发者提供实用的技术指南。
|
1天前
|
XML Java Android开发
如何美化android程序:自定义ListView背景
如何美化android程序:自定义ListView背景
|
1天前
|
搜索推荐 Android开发
自定义Android标题栏TitleBar布局
自定义Android标题栏TitleBar布局
|
17天前
|
Android开发 芯片
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
Android源代码定制:移除无用lunch|新建lunch|自定义customize.mk
26 3
|
17天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
40 1
|
17天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
34 0
|
17天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
29 0
|
17天前
|
网络协议 Shell Android开发
Android 深入学习ADB调试原理(1)
Android 深入学习ADB调试原理(1)
26 1