如何获取组件的宽和高

由于Android程序的运行机制决定了无法在组件类外部使用getWidth和getHeight方法获得高度与宽度,必须使用View.getMeasuredWidth和View.getMeasuredHeight方法获取当前组件的宽度和高度。

代码:

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控件都加载完了才能获取到这些。
 }

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

}

 

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

1.屏幕尺寸,源代码如下:

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

 这段代码可以插入到Activity的onCreate()函数中。

 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为标题栏高度

 

你可能感兴趣的:(如何获取组件的宽和高)