首页 > 解决方案 > 如何在android studio中以编程方式将按钮大小设置为全屏?

问题描述

我正在使用约束布局,我想构建一个 ButtonImage,当我运行应用程序时,它的大小变为全屏。我知道我可以使用 android:layout_width="match_parent" and android:layout_height="match_parent" xml 来制作它,但是如果我想移动 ButtonImage,它会创建一个宽大的滞后图像,因为 ButtonImage 想要填充父级。如何为每种类型的显示设置全屏?

标签: androidandroid-constraintlayout

解决方案


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

推荐阅读