Android 自定义View 中的OnMeasure的用法

  1. 作用:根据父容器传递跟子容器的大小要求来确定子容器的大小。
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)的参数说明和MeasureSpec类的说明:

通过MeasureSpec这个类可以获取父View传递过来的一些信息,包括MODE、SIZE属性。这里做一下说明

    • MODE:分为一下三种类别,
      • AT_MOST:子容器可以是声明大小内的任意大小
      • EXACTLY:父容器已经为子容器确定的大小,子容器应该遵守
      • UNSPECIFIED:父容器对子容器没有做任何限制,子容器可以任意大小
    • SIZE是父容器为子容器提供的大小
      • 当MODE为AT_MOST时,SIZE大小为父容器所能提供的最大值。
      • 当MODE为EXACTLY时,SIZE为父容器提供的限制值。
      • 当MODE为UNSPECIFIED时,大小为0,SIZE完全由子容器的大小决定。

实例说明:

(1)首先我们新建一个View,重写onMeasure()方法.

package com.example.measuredemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MeasureView extends View {
	private final static String TAG = "MeasureView";

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

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(measureSize(widthMeasureSpec),
				measureSize(heightMeasureSpec));
	}

	private int measureSize(int iMeasureSpec) {
		int result = 0;// 结果
		int specMode = MeasureSpec.getMode(iMeasureSpec);
		int specSize = MeasureSpec.getSize(iMeasureSpec);

		switch (specMode) {
		// 子控件为声明大小内的任意大小
		case MeasureSpec.AT_MOST:
			Log.d(TAG, "AT_MOST----->子控件为声明大小内的任意大小");
			Log.d(TAG, "大小为----->" + specSize);
			result = specSize;
			break;
		// 父容器已经为子容器设置了尺寸,子容器应当服从这些边界,不论子容器想要多大的空间
		case MeasureSpec.EXACTLY:
			Log.d(TAG, "EXACTLY----->父容器已经为子容器设置了尺寸,子容器应当服从这些边界");
			Log.d(TAG, "大小为----->" + specSize);
			result = specSize;
			break;
		// 父容器对于子容器没有任何限制,子容器想要多大就多大. 所以完全取决于子view的大小
		case MeasureSpec.UNSPECIFIED:
			Log.d(TAG, "UNSPECIFIED----->父容器对于子容器没有任何限制");
			Log.d(TAG, "大小为----->" + specSize);
			result = 200;
			break;
		default:
			break;
		}
		return result;
	}
}
(2)然后我们在布局文件中写如下布局:

<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <com.example.measuredemo.MeasureView
            android:layout_width="999dp"
            android:layout_height="80dp"
            android:background="#eeaa11" />
    </HorizontalScrollView>

</RelativeLayout>


这个是运行后的结果,从上面我们可以看出:

  • MeasureView的父容器为HorizontalScrollView,HorizontalScrollView对它的子容器的width是没有任何限制的,所以当width设置为999dp时子容器的宽度去决定于自己的宽度(属于UNSPECIFIED),size大小为0.
  • 第一个CustomView1的宽度设置为80dp时(320px),这时候父容器为它设定了具体的值(属于EXACTLY)
(3)修改布局文件:

<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <com.example.measuredemo.MeasureView
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:background="#eeaa11" />

</RelativeLayout>

MeasureView的高度设置为wrap_content时,这时父容器为它做了限制,只要在父容器设置的尺寸(2268px)之内都可以,也就是父控件的高度大小。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值