您的位置:首页 > 移动开发 > Android开发

Android 自定义view基础(三)

2017-04-24 10:18 218 查看
本章节主要讲解View的onMeasure()方法

onMeasure()方法的作用就是测量View需要多大的空间,就是宽和高。如果继承自原有控件,则一般不需要重写此方法
 


demo:

1.xml

<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" >

    <com.example.eventbusdemo.MyView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#FFFF77" />

</RelativeLayout>


2.自定义view 只是重写了onMeasure方法没有操作
看看情况


package com.example.eventbusdemo;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

public class MyView extends View{

//构造方法1
public MyView(Context context) {
super(context);
}

//构造方法2
public MyView(Context context,AttributeSet attrs){
super(context, attrs);
}

//构造方法3
public MyView(Context context,AttributeSet attrs,int defStyleAttr){
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}


3.运行结果






当xml为

<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" >

    <com.example.eventbusdemo.MyView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="#FFFF77" />

</RelativeLayout>


运行结果






当xml为

<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" >

    <com.example.eventbusdemo.MyView

        android:layout_width="200dp"

        android:layout_height="200dp"

        android:background="#FFFF77" />

</RelativeLayout>


运行结果






由此可以得一下结论

1、View默认的测量规则是android:layout_width和android:layout_height为match_parent或者wrap_content时,是填充全屏的。 

2、android:layout_width和android:layout_height设置为具体值时,那么是多少,宽高就是多少。


所以自定义view时要对onMeasure方法进行操作。



当自定义view为

package com.example.eventbusdemo;

import android.content.Context;

import android.util.AttributeSet;

import android.view.View;

public class MyView extends View{

//构造方法1
public MyView(Context context) {
super(context);
}

//构造方法2
public MyView(Context context,AttributeSet attrs){
super(context, attrs);
}

//构造方法3
public MyView(Context context,AttributeSet attrs,int defStyleAttr){
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(300, 500);
}

}




即在onMeasure中规定了view的大小

setMeasuredDimension(300, 500);


此时xml中无论宽度高度怎样设置 view的大小都会是300X500(px)








所以

结论: 

onMeasure方法的作用就是计算出自定义View的宽度和高度。这个计算的过程参照父布局给出的大小,以及自己特点算出结果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息