Android开发动态获取控件宽高

[java]  view plain  copy
  1. 最近做项目自定义控件时候想获取控件中元素参数,但各种失败,网上搜了下,自己也总结一下。  
[java]  view plain  copy
  1. package com.example.androidtest_02;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewTreeObserver;  
  11. import android.view.ViewTreeObserver.OnGlobalLayoutListener;  
  12. import android.view.WindowManager.LayoutParams;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15.   
  16. /** 
  17.  *  
  18.  * 这个activity测试 : 1.Activity的生命周期 2.程序动态获取控件的宽高 
  19.  *  
  20.  * 结论:activity启动时候:onCreate()-->onStart()--->onResum()-->onAttachedToWindow()--> 
  21.  * onWindowFocusChanged() 
  22.  *  
  23.  * 当另外一个activity be in front of 
  24.  * 的时候:onPause()--->onWindowFocusChanged()-->onStop() 
  25.  *  
  26.  * 当第一个activity恢复到最前面时候:onRestart()--->onStart()--->onResum()---> 
  27.  * onWindowFocusChanged() 
  28.  *  
  29.  * 当activityfinish的时候:onPause()-->onWindowFocusChanged()-->onStop()-->onDestroy( 
  30.  * ) 
  31.  *  
  32.  *  
  33.  * onAttachedToWindow()此时打印出了控件宽高,其他地方的打印为0 ,so,activity全面加载完了之后才会获取到控件的宽高, 
  34.  * 当我们需要获取控件的宽高时,需要等activity加载完。 
  35.  *  
  36.  * 第二种与第三种方式:即使实在oncreate()方法里面声明了,回调里面的内容也只能在onResume()方法被调用后才能得到执行。 
  37.  */  
  38. public class MainActivity extends Activity implements OnClickListener {  
  39.   
  40.     public void printLog(String s) {  
  41.   
  42.         Log.e("-------------------------------", s);  
  43.     }  
  44.   
  45.     Button btn1;  
  46.     Button btn2;  
  47.     TextView textview1;  
  48.   
  49.     /** 
  50.      * 第一种获取宽高的方式。 
  51.      */  
  52.     int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
  53.     int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);  
  54.   
  55.     @Override  
  56.     protected void onCreate(Bundle savedInstanceState) {  
  57.         super.onCreate(savedInstanceState);  
  58.         setContentView(R.layout.activity_main);  
  59.   
  60.         btn1 = (Button) findViewById(R.id.btn1);  
  61.         btn1.setOnClickListener(this);  
  62.         btn2 = (Button) findViewById(R.id.btn2);  
  63.         textview1 = (TextView) findViewById(R.id.textview1);  
  64.   
  65.         textview1.measure(w, h);  
  66.         // int height =textview1.getMeasuredHeight();  
  67.         // int width =textview1.getMeasuredWidth();  
  68.         printLog("直接获取宽高:width:" + textview1.getWidth() + "---->height:"  
  69.                 + textview1.getHeight());  
  70.         printLog("使用mesure()获取宽高:width:" + textview1.getWidth()  
  71.                 + "---->height:" + textview1.getHeight());  
  72.   
  73.         // 即使实在oncreate()方法里面声明了,回调里面的内容也只能在onResume()方法被调用后才能得到执行。  
  74.   
  75.         // 第二种获取宽高的方式  
  76.         ViewTreeObserver viewTreeObserver = textview1.getViewTreeObserver();  
  77.         viewTreeObserver  
  78.                 .addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {  
  79.   
  80.                     @Override  
  81.                     public boolean onPreDraw() {  
  82.   
  83.                         printLog("使用ViewTreeObserver 获取宽高:width:"  
  84.                                 + textview1.getMeasuredWidth() + "---->height:"  
  85.                                 + textview1.getMeasuredHeight());  
  86.   
  87.                         return true;  
  88.                     }  
  89.                 });  
  90.   
  91.         // 第三种获取宽高的方式  
  92.         ViewTreeObserver vtb1 = textview1.getViewTreeObserver();  
  93.         vtb1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
  94.   
  95.             @Override  
  96.             public void onGlobalLayout() {  
  97.                 textview1.getViewTreeObserver().addOnGlobalLayoutListener(this);  
  98.                 printLog("使用OnGlobalLayoutListener 获取宽高:width:"  
  99.                         + textview1.getWidth() + "---->height:"  
  100.                         + textview1.getHeight());  
  101.             }  
  102.         });  
  103.   
  104.     }  
  105.   
  106.     @Override  
  107.     public boolean onCreateOptionsMenu(Menu menu) {  
  108.         // Inflate the menu; this adds items to the action bar if it is present.  
  109.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  110.         return true;  
  111.     }  
  112.   
  113.     @Override  
  114.     protected void onStart() {  
  115.         super.onStart();  
  116.         printLog("onstart()");  
  117.         printLog("width:" + textview1.getWidth() + "---->height:"  
  118.                 + textview1.getHeight());  
  119.   
  120.     }  
  121.   
  122.     @Override  
  123.     protected void onResume() {  
  124.         super.onResume();  
  125.         printLog("onResume()");  
  126.         printLog("width:" + textview1.getWidth() + "---->height:"  
  127.                 + textview1.getHeight());  
  128.     }  
  129.   
  130.     @Override  
  131.     public void onAttachedToWindow() {  
  132.         super.onAttachedToWindow();  
  133.         printLog("onAttachedToWindow()");  
  134.         printLog("width:" + textview1.getWidth() + "---->height:"  
  135.                 + textview1.getHeight());  
  136.     }  
  137.   
  138.     @Override  
  139.     public void onWindowFocusChanged(boolean hasFocus) {  
  140.         super.onWindowFocusChanged(hasFocus);  
  141.         printLog("onWindowFocusChanged()");  
  142.         printLog("width:" + textview1.getWidth() + "---->height:"  
  143.                 + textview1.getHeight());// 此时打印出了控件宽高,其他地方的打印为0  
  144.     }  
  145.   
  146.     @Override  
  147.     protected void onRestart() {  
  148.         super.onRestart();  
  149.         printLog("onRestart()");  
  150.     }  
  151.   
  152.     @Override  
  153.     public void onWindowAttributesChanged(LayoutParams params) {  
  154.         super.onWindowAttributesChanged(params);  
  155.         printLog("onWindowAttributesChanged()");  
  156.     }  
  157.   
  158.     @Override  
  159.     protected void onPause() {  
  160.         super.onPause();  
  161.         printLog("onPause()");  
  162.     }  
  163.   
  164.     @Override  
  165.     protected void onStop() {  
  166.         super.onStop();  
  167.         printLog("onStop()");  
  168.     }  
  169.   
  170.     @Override  
  171.     protected void onDestroy() {  
  172.         super.onDestroy();  
  173.         printLog("onDestroy()");  
  174.     }  
  175.   
  176.     @Override  
  177.     public void onClick(View v) {  
  178.   
  179.         if (v.equals(btn1)) {  
  180.             printLog("+++++++++++++++++++马上要跳转了++++++++++++++++++");  
  181.             Intent intent = new Intent(MainActivity.this, Activity1.class);  
  182.             startActivity(intent);  
  183.         } else if (v.equals(btn2)) {  
  184.   
  185.             printLog("+++++++++++++++++++马上finish了++++++++++++++++++");  
  186.             this.finish();  
  187.         }  
  188.     }  
  189.   
  190.     @Override  
  191.     public void onBackPressed() {  
  192.   
  193.         printLog("+++++++++++++++++++我点了返回键,马上要跳转了++++++++++++++++++");  
  194.   
  195.     }  
  196.   
  197. }  

Android开发动态获取控件宽高_第1张图片

你可能感兴趣的:(Android开发动态获取控件宽高)