屏幕获取、控件获取、设置宽高

控件获取

1、ViewTreeObserver vto = video.getViewTreeObserver();   
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override  
            public void onGlobalLayout() { 
            video.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            video.getHeight();
            video.getWidth();
            Toast.makeText(VideoActivity.this, "视频高读。。。。。" + video.getHeight(), Toast.LENGTH_LONG).show();
            }   
        });


2、获取屏幕宽高,设置控件宽高

DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
screenWidth = metric.widthPixels; // 屏幕宽度(像素)
screenHeight = metric.heightPixels; // 屏幕高度(像素)
density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)

LayoutParams para;
para = tv_when_long.getLayoutParams();
para.width = (int) (screenWidth*0.2)+1;
tv_unitprice.setLayoutParams(para);
para.width = (int) (screenWidth*0.2);
tv_time.setLayoutParams(para);


3、获取textview宽高

int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
		tv.measure(w, h);
		int width = tv.getMeasuredWidth();
		int high = tv.getMeasuredHeight();

设置宽高

ViewGroup.LayoutParams para = tv.getLayoutParams();
        para.width = width;
        tv.setLayoutParams(para);





你可能感兴趣的:(屏幕获取、控件获取、设置宽高)