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的宽度和高度。这个计算的过程参照父布局给出的大小,以及自己特点算出结果。
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的宽度和高度。这个计算的过程参照父布局给出的大小,以及自己特点算出结果。
相关文章推荐
- Android基础教程(九)之自定义下拉菜单模式----Spinner与setDropDownViewResource的应用
- Android基础教程(九)之自定义下拉菜单模式----Spinner与setDropDownViewResource的应用
- 0919Android基础自定义ViewGroup
- android自定义View基础之------android坐标系
- Android绘图机制(一)——自定义View的基础属性和方法
- Android从基础做起——自定义View(随手移动的小球)
- 0916Android基础自定义View进度条专题
- android基础之自定义view
- 0917Android基础自定义View(Path)
- Android自定义View基础篇(二)
- Android基础教程(九)之自定义下拉菜单模式----Spinner与setDropDownViewResource的应用
- android 自定义View(一、基础认识)
- android 自定义view 前的基础知识
- Android 自定义ViewGroup(一) 基础
- android基础(一)View与六大布局之自定义(下)
- android基础学习之自定义view的简单原理
- Android自定义View基础(1)
- Android自定义View——基础知识篇
- Android绘图机制(一)——自定义View的基础属性和方法