是否可以使用 java 将使用 XML 创建的按钮居中?
我知道它们可以设置为不可见:
button1.setVisibility(View.GONE);
基本上,我有一个 if 语句,我希望它这样做:
If (possibility == true) {
//center button horizontally and stay the same vertically
}
是否可以使用 java 将使用 XML 创建的按钮居中?
我知道它们可以设置为不可见:
button1.setVisibility(View.GONE);
基本上,我有一个 if 语句,我希望它这样做:
If (possibility == true) {
//center button horizontally and stay the same vertically
}
看起来你应该能够做一些事情来达到这个效果:
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
params.addRule(RelativeLayout.BELOW, R.id.id_to_be_below);
button.setLayoutParams(params); //causes layout update
相对布局有时很有趣,所以你可能不得不玩弄这些规则,直到你完全按照你想要的方式得到它,但这会让你走上正轨!