我正在使用 Android Giraffe,我想从按钮获取 LayoutParams,然后更改其中一个值,然后设置新参数,如下所示:
//get the current params
LayoutParams params = button.getLayoutParams();
//change something in the params
//set the button with the new params
button.setLayoutParams(params);
但是,“LayoutParams”本身并不是一个选项。这些选项特定于视图。例如:
ConstraintLayout.LayoutParams
ViewGroup.LayoutParams
ActionBar.LayoutParams
etc...
这些选项之一是否只适用于按钮?没有 Button.LayoutParams。如何完成我想做的事情?
我想通了。您只需将 LayoutParams 类型转换为小部件所在的布局类型:
//get the current params
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) button.getLayoutParams();
//change something in the params
params.setMargins(0, 0, 0, 200);
//set the button with the new params
button.setLayoutParams(params);