Android 自定义View 中的OnMeasure的用法
来源:互联网 发布:人工智能的利弊 编辑:程序博客网 时间:2024/05/22 05:29
- 作用:根据父容器传递跟子容器的大小要求来确定子容器的大小。
- 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);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.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)
<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
- Android 自定义View 中的OnMeasure的用法
- Android 自定义View 中的OnMeasure的用法
- Android 自定义View 中的OnMeasure的用法
- Android自定义View onMeasure
- android 自定义view-onMeasure
- android 自定义view onMeasure()
- android自定义View中onMeasure的使用
- Android 自定义View onMeasure 的理解
- Android 自定义View onMeasure方法的实现
- 自定义View的onMeasure()
- android 自定义view之onMeasure
- Android 自定义View onMeasure理解
- android 自定义view中onMeasure()
- Android自定义View-onMeasure介绍
- android 自定义View之View的测量(onMeasure()方法)
- android 自定义View之View的测量(onMeasure()方法)
- 自定义View的OnMeasure理解
- 自定义View的onMeasure过程
- Mysql主从复制和读写分离方案分析
- BroadCastReciver
- jQuery在页面加载时动态修改图片尺寸
- iOS:Scale to Fill,Aspect Fit,Aspect Fill
- 三种字符串逆序的方法
- Android 自定义View 中的OnMeasure的用法
- PCB板热设计
- Android SDK Manager国内无法更新的解决方案
- Jboss wildfly部署在linux环境上无法访问的问题
- eclipse提示Syntax error, insert “EnumBody” to complete EnumDeclaration
- 数组中只出现一次的数字
- Servlet生命周期与工作原理
- Qt Creator创建 的 project工程文件介绍
- 中缀表达式转换成前缀表达式和后缀表达式的极其简单方法