Android 动态计算控件大小

// Android 动态计算控件大小

参考:在安卓中设置控件宽高是屏幕的一半

TextView tv = (TextView) findViewById(R.id.tv);
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
//拿到布局参数 
LayoutParams l = tv.getLayoutParams();
l.width=d.getWidth()/3;
l.height=d.getHeight()/3;

// Android获取屏幕的高度和宽度

参考链接1:https://www.cnblogs.com/pswzone/archive/2012/03/11/2389993.html

参考链接2:https://www.cnblogs.com/guanxinjing/p/10396208.html

1、
WindowManager wm = (WindowManager) getContext()
                    .getSystemService(Context.WINDOW_SERVICE);
 
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
2、
WindowManager wm = this.getWindowManager();
 
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight();
3、
// 获取包含导航栏与状态栏的设备的实际屏幕宽高
// 根据此设备屏幕的实际大小获取显示指标,尺寸会根据显示器的当前旋转进行调整。
private void getDeviceRealHeight(Context context){
        DisplayMetrics outMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getRealMetrics(outMetrics);
        int deviceRealHeightPixels = outMetrics.heightPixels;
 }

其他:

https://blog.csdn.net/xiey94/article/details/78963811

https://blog.csdn.net/v2810769/article/details/77920885

发表评论

zh_CNChinese
zh_CNChinese