如何獲取組件的寬和高

因爲Android程序的運行機制決定了沒法在組件類外部使用getWidth和getHeight方法得到高度與寬度,必須使用View.getMeasuredWidth和View.getMeasuredHeight方法獲取當前組件的寬度和高度。 app

代碼: ide

public class AndrodTActivity extends Activity implements OnClickListener {
 /** Called when the activity is first created. */
 View v;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  v = findViewById(R.id.button1);
  v.setOnClickListener(this);
 } 函數

 @Override
 public void onClick(View v) {
  //獲取組件相對於屏幕的x,y座標,若需得到在當前窗口內的座標 ,則view.getLocationInWindow(location);
  int location[] = new int[2];
  v.getLocationOnScreen(location);
  int x = location[0];
  int y = location[1];
  //類的內部調用getWidth()與getHeight()值能夠正常返回
  int w = v.getWidth();
  int h = v.getHeight();
  //下面代碼片斷返回的值與類內部調用一致
  v.measure(0, 0);
  int measuredWidth = v.getMeasuredWidth();
  int measuredHeight = v.getMeasuredHeight();
  System.out.println();//若是在Activity的OnCreate()事件輸出那些參數,是全爲0,要等UI控件都加載完了才能獲取到這些。
 } this

 @Override
 protected void onStart() {
  super.onStart();
  int width = v.getWidth();
  int height = v.getHeight();
  System.out.println();//類的外部調用getWidth()與getHeight()值都返回0
 } spa

} server

 

        //------------------------------------------------方法一
        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());
            } 
        });  rem

1.屏幕尺寸,源代碼以下:

DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int screenWidth=metrics.widthPixels;            //屏幕寬度
    int .screenHeight=metrics.heightPixels;        //屏幕高度 get

 這段代碼能夠插入到Activity的onCreate()函數中。 it

 2.獲取標題欄、狀態欄高度:

Rect rect = new Rect();
    Window win = this.getWindow();
    win.getDecorView().getWindowVisibleDisplayFrame(rect);
    int statusBarHeight = rect.top;
    int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
    int titleBarHeight = contentViewTop - Variable.statusBarHeight; 
  

//statusBarHeight爲狀態欄高度,titleBarHeight爲標題欄高度

相關文章
相關標籤/搜索