android - 如何在android studio中以编程方式将按钮大小设置为全屏?
问题描述
我正在使用约束布局,我想构建一个 ButtonImage,当我运行应用程序时,它的大小变为全屏。我知道我可以使用
android:layout_width="match_parent" and android:layout_height="match_parent"
xml 来制作它,但是如果我想移动 ButtonImage,它会创建一个宽大的滞后图像,因为 ButtonImage 想要填充父级。如何为每种类型的显示设置全屏?
解决方案
ConstraintLayout.LayoutParams pp = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT);
Button b = new Button(getApplicationContext());
b.setLayoutParams(pp);
这会将匹配父参数设置为按钮 b。根据需要更改它以匹配您的代码。请注意,如果您有线性或相对或其他任何东西,只需更改
ConstraintLayout.LayoutParams
至
YourLayout.LayoutParams
推荐阅读
- laravel - Laravel - 为什么使用尾随空格保存 CHAR 列?
- php - 如何使用 domcrawler 替换特定元素的文本
- python - aws ec2 不在 django react 应用程序中提供静态文件
- python - 仅包含第二对元素的列表
- excel - 将工作簿复制到新工作簿时出现 VBA 错误
- r - 在不同深度访问相同 XML 的多个元素以转换为 R tibble
- android - Google APIs 获取所有应用的应用收入
- python - 如何使用多个传感器转换 time.series 数据的 csv 文件?
- javascript - 允许所有国际字符但不允许符号的正则表达式
- css - 如何删除侧边栏中的多余空间?