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. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章