---
title: "Android自定义View研究：View的大小"
author: "mywebcode"
date: "2011-12-04T16:36:00Z"
tags: []
slug: "android-view-view"
draft: false
lastmod: "2013-05-08T10:25:15Z"
id: 1862803
url: "https://mywebcode.iteye.com/blog/1862803"
---

# Android自定义View研究：View的大小

Android开发View是一个基本的视图界面，但是如何做一个自定义的View，那View的大小是多少呢?这小节我就研究下View的大小。通过LogCat来研究View的大小是怎样确定的。好了，直接切入正题吧.

　　一、 在Activity中直接new HelloView 时View的大小。

　　View的大小获取可以用其中的两种方法获取：

　　this.getHeight()：获取View的高

　　this.getWidth()：获取View的宽

　　我们可以做一个猜想，View的大小是在什么时候确定的，是在new一个View的时候还是在onDraw()的时候?还是在其他时候?为了研究这个，我们分别在构造函数和onDraw中打上Log补丁(这个只是个人习惯的称呼)。

　　--- &gt;HelloVew.java

　　public HelloView(Context context){

　　super(context);

　　Log.v("HelloView(Context context)","" + this.getHeight()+ " " + this.getWidth());

　　}

　　/\**

　　* 这个是我们要在XML中初始化用的

　　* \*/

　　public HelloView(Context context,AttributeSet attrs){

　　super(context, attrs);

　　Log.v("HelloView(Context context,AttributeSet attrs)","" + this.getHeight()+ " " + this.getWidth());

　　}

　　/\**

　　* 绘制View

　　* \*/

　　protected void onDraw(Canvas canvas){

　　Log.v("onDraw(Canvas canvas)","" + this.getHeight()+ " " + this.getWidth());

　　canvas.drawColor(Color.WHITE);

　　myUseBitmapFactory(canvas);

　　myUseBitmapDrawable(canvas);

　　myUseInputStreamandBitmapDrawable(canvas);

　　}

　　运行：

　　![](http://images.cnblogs.com/cnblogs_com/lovewf/5.newView%E8%8E%B7%E5%BE%97View%E5%A4%A7%E5%B0%8F.png)

　　我们观察可以发现，new View 的时候并没有确定了View的大小，并且系统就没有调用(Context context)这个构造函数。

　　也就是说View大小是在new View之后OnDraw之前确定的，那onDraw之前的又有那些方法了，呵呵，我们试着override这个方法试试：

　　protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

　　// TODO Auto-generated method stub

　　super.onMeasure(widthMeasureSpec, heightMeasureSpec);

　　Log.v("onMeasure","" + this.getHeight()+ " " + this.getWidth());

　　}

　　运行：

　　![](http://images.cnblogs.com/cnblogs_com/lovewf/5onMeasure%E6%96%B9%E6%B3%95.png)

　　我们观察发现：onMeasure方法运行了两次：第一次宽和高都是0，但是第二次就变了，是不是可以说是在这个方法中确定的，但是实际上不一定会是这么回事，这个我们放在以后研究。这里我们只需要知道不是在new View时确定的就好了。

　　**二、在XML中定义时View大小**

　　这个我们直接上代码：

　　main.xml文件修改：

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout\_width="fill\_parent"

android:layout\_height="fill\_parent"

&gt;

&lt;TextView

android:layout\_width="fill\_parent"

android:layout\_height="wrap\_content"

android:text="@string/hello"

/&gt;

&lt;view class="com.fxhy.stady.HelloView"

android:layout\_width="50dip"

android:layout\_height="120dip"

/&gt;

&lt;/LinearLayout&gt;

　　mainActivity ：

/\**

\* 使用自定义的View

\* \*/

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);// 使用自定义的View

}

}

　　运行：

　　![](http://images.cnblogs.com/cnblogs_com/lovewf/5.xml%E5%B8%83%E5%B1%80%E5%AE%9A%E4%B9%89View%E5%A4%A7%E5%B0%8F%E6%98%BE%E7%A4%BA.png)

　　![](http://images.cnblogs.com/cnblogs_com/lovewf/5.xml%E5%B8%83%E5%B1%80%E5%AE%9A%E4%B9%89View%E5%A4%A7%E5%B0%8F.png)

　　我们发现，和我们Xml中定义的大小一样，哈哈，有兴趣的可以自己测试测试。