Android初级教程_获取Android控件的宽和高 .

我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例:

首先我们自己写一个控件,这个控件非常简单:

 

  1. public class MyImageView extends ImageView {  
  2.   
  3.     public MyImageView(Context context, AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.     }  
  6.     public MyImageView(Context context) {  
  7.         super(context);  
  8.     }  
  9.       
  10.     @Override  
  11.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  12.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  13.         System.out.println("onMeasure 我被调用了"+System.currentTimeMillis());  
  14.     }  
  15.       
  16.     @Override  
  17.     protected void onDraw(Canvas canvas) {  
  18.         super.onDraw(canvas);  
  19.         System.out.println("onDraw 我被调用了"+System.currentTimeMillis());  
  20.     }  
  21.   
  22. }  
public class MyImageView extends ImageView {

	public MyImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public MyImageView(Context context) {
		super(context);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		System.out.println("onMeasure 我被调用了"+System.currentTimeMillis());
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		System.out.println("onDraw 我被调用了"+System.currentTimeMillis());
	}

}

 

 

布局文件:

 

  1. <com.test.MyImageView  
  2.     android:id="@+id/imageview"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:src="@drawable/test" />  
    <com.test.MyImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/test" />


测试的Activity的onCreate():

 

 

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);          
  5.     System.out.println("执行完毕.."+System.currentTimeMillis());  
  6. }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        System.out.println("执行完毕.."+System.currentTimeMillis());
    }

现在我们现在来看一下结果:

 


说明等onCreate方法执行完了,我们定义的控件才会被度量(measure),所以我们在onCreate方法里面通过view.getHeight()获取控件的高度或者宽度肯定是0,因为它自己还没有被度量,也就是说他自己都不知道自己有多高,而你这时候去获取它的尺寸,肯定是不行的.

 

现在碰到这个问题我们不能不解决,在网上找到了如下办法:

 

  1. //------------------------------------------------方法一   
  2. int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  3. int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  4. imageView.measure(w, h);  
  5. int height =imageView.getMeasuredHeight();  
  6. int width =imageView.getMeasuredWidth();  
  7. textView.append("\n"+height+","+width);  
  8.   
  9.   
  10.   
  11.   
  12. //-----------------------------------------------方法二   
  13. ViewTreeObserver vto = imageView.getViewTreeObserver();  
  14. vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
  15.     public boolean onPreDraw() {  
  16.         int height = imageView.getMeasuredHeight();  
  17.         int width = imageView.getMeasuredWidth();  
  18.         textView.append("\n"+height+","+width);  
  19.         return true;  
  20.     }  
  21. });  
  22. //-----------------------------------------------方法三      
  23. ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
  24. vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
  25.     @Override    
  26.     public void onGlobalLayout() {  
  27.         imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
  28.         textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
  29.     }    
  30. });    
        //------------------------------------------------方法一
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        imageView.measure(w, h);
        int height =imageView.getMeasuredHeight();
        int width =imageView.getMeasuredWidth();
        textView.append("\n"+height+","+width);
        
        
        

        //-----------------------------------------------方法二
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = imageView.getMeasuredHeight();
                int width = imageView.getMeasuredWidth();
                textView.append("\n"+height+","+width);
                return true;
            }
        });
        //-----------------------------------------------方法三   
        ViewTreeObserver vto2 = imageView.getViewTreeObserver();  
        vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override  
            public void onGlobalLayout() {
            	imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
                textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());
            }  
        });  


这三个方法是哪里找到现在已经忘了.

 

 

现在要讨论的是当我们需要时候使用哪个方法呢?

现在把测试的Activity改成如下:

 

  1. @Override  
  2.   public void onCreate(Bundle savedInstanceState) {  
  3.       super.onCreate(savedInstanceState);  
  4.       setContentView(R.layout.main);  
  5.       final ImageView imageView = (ImageView) findViewById(R.id.imageview);        
  6.         
  7.       //------------------------------------------------方法一   
  8.       int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  9.       int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);  
  10.       imageView.measure(w, h);  
  11.       int height =imageView.getMeasuredHeight();  
  12.       int width =imageView.getMeasuredWidth();  
  13.       textView.append("\n"+height+","+width);  
  14.         
  15.       System.out.println("执行完毕.."+System.currentTimeMillis());  
  16.   }  
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);      
        
        //------------------------------------------------方法一
        int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        imageView.measure(w, h);
        int height =imageView.getMeasuredHeight();
        int width =imageView.getMeasuredWidth();
        textView.append("\n"+height+","+width);
        
        System.out.println("执行完毕.."+System.currentTimeMillis());
    }


Android初级教程_获取Android控件的宽和高 ._第1张图片

 

 

接着来看下面几种方式输出结果:

把测试Activity改成如下:

 

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5.     final ImageView imageView = (ImageView) findViewById(R.id.imageview);  
  6. -----------------------------------------------方法二  
  7.     ViewTreeObserver vto = imageView.getViewTreeObserver();  
  8.     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
  9.         public boolean onPreDraw() {  
  10.             int height = imageView.getMeasuredHeight();  
  11.             int width = imageView.getMeasuredWidth();  
  12.             textView.append("\n"+height+","+width);  
  13.             return true;  
  14.         }  
  15.     });  
  16. }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);
		//-----------------------------------------------方法二
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = imageView.getMeasuredHeight();
                int width = imageView.getMeasuredWidth();
                textView.append("\n"+height+","+width);
                return true;
            }
        });
    }


结果如下:

 


 

方法三就不再测试了同方法二!!!

 

那么方法而和方法三在执行上有什么区别呢?

我们在布局文件中加入一个TextView来记录这个控件的宽高.

 

  1. <ScrollView  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content" >  
  4.   
  5.     <TextView  
  6.         android:id="@+id/text"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content" />  
  9. </ScrollView>  
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </ScrollView>


先来测试方法而:

 

 

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5.     final ImageView imageView = (ImageView) findViewById(R.id.imageview);  
  6. -----------------------------------------------方法二  
  7.     ViewTreeObserver vto = imageView.getViewTreeObserver();  
  8.     vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
  9.         public boolean onPreDraw() {  
  10.             int height = imageView.getMeasuredHeight();  
  11.             int width = imageView.getMeasuredWidth();  
  12.             textView.append("\n"+height+","+width);  
  13.             return true;  
  14.         }  
  15.     });  
  16. }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);
		//-----------------------------------------------方法二
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                int height = imageView.getMeasuredHeight();
                int width = imageView.getMeasuredWidth();
                textView.append("\n"+height+","+width);
                return true;
            }
        });
    }


结果如下:

 

Android初级教程_获取Android控件的宽和高 ._第2张图片

 

我们再来测试方法三

 

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5.     final ImageView imageView = (ImageView) findViewById(R.id.imageview);  
  6.     //-----------------------------------------------方法三      
  7.     ViewTreeObserver vto2 = imageView.getViewTreeObserver();    
  8.     vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
  9.         @Override    
  10.         public void onGlobalLayout() {  
  11.             imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);    
  12.             textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());  
  13.         }    
  14.     });    
  15. }  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final ImageView imageView = (ImageView) findViewById(R.id.imageview);
        //-----------------------------------------------方法三   
        ViewTreeObserver vto2 = imageView.getViewTreeObserver();  
        vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override  
            public void onGlobalLayout() {
            	imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);  
                textView.append("\n\n"+imageView.getHeight()+","+imageView.getWidth());
            }  
        });  
    }


输出结果如下:

 

Android初级教程_获取Android控件的宽和高 ._第3张图片

 

我想这方法二和方法三之间的区别就不用说了吧.

 总结:那么需要获取控件的宽高该用那个方法呢?

方法一: 比其他的两个方法多了一次计算,也就是多调用了一次onMeasure()方法,该方法虽然看上去简单,但是如果要目标控件计算耗时比较大的话(如listView等),不建议使用.

方法二,它的回调方法会调用很多次,并且滑动TextView的时候任然会调用,所以不建议使用.

方法三,比较合适.

当然,实际应用的时候需要根据实际情况而定.

 

谢谢!! 欢迎转载:http://blog.csdn.net/johnny901114/article/details/7839512

 

你可能感兴趣的:(android,宽和高)