Android中在代码中设置控件的宽和高
16lz
2021-01-25
//在代码中设置控件大小的方法
private Button mbtn;
mbtn = (Button) findViewById(R.id.btn_test);
LayoutParams lp;
lp=mbtn.getLayoutParams();
lp.width=100;
lp.height=200;
mbtn.setLayoutParams(lp);
//在代码中设置界面大小的方法:
Display display = getWindowManager().getDefaultDisplay(); // 为获取屏幕宽、高
Window window = getWindow();
LayoutParams windowLayoutParams = window.getAttributes(); // 获取对话框当前的参数值
windowLayoutParams.width = (int) (display.getWidth() * 0.7); // 宽度设置为屏幕的0.95
windowLayoutParams.height = (int) (display.getHeight() * 0.1); // 高度设置为屏幕的0.6
windowLayoutParams.alpha = 0.5f;// 设置透明度
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- ImageView宽度填满屏幕,高度自适应
- GPS在Android的使用
- 【Android(安卓)开发教程】使用Intent传递数据
- Android(安卓)用纯代码实现复杂界面
- Android让屏幕保持常亮,不熄屏的三种方法
- android将path拆分为多个path
- android activity-alias 的作用
- 《第一行代码-Android》学习笔记(八)