我正在尝试在中心应用程序上放一个按钮...有人可以向我解释这个代码到底在做什么?
RelativeLayout.LayoutParams BD = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
BD.addRule(RelativeLayout.CENTER_HORIZONTAL);
BD.addRule(RelativeLayout.CENTER_VERTICAL);
此代码的作用是
RelativeLayout.LayoutParams BD = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
上线告诉视图仅占据所需的空间。
BD.addRule(RelativeLayout.CENTER_HORIZONTAL);
BD.addRule(RelativeLayout.CENTER_VERTICAL);
上面的两行告诉Layoutmanager将此视图保持在水平和垂直方向。
您应该阅读有关Relativelayout和Relativelayoutparams。
RelativeLayout relativeLayout;
Button btnNewButton;
relativeLayout = new RelativeLayout(getApplicationContext());
btnNewButton = new Button(getApplicationContext());
btnNewButton.setText("Button Text");
RelativeLayout.LayoutParams BD = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
BD.addRule(RelativeLayout.CENTER_HORIZONTAL);
BD.addRule(RelativeLayout.CENTER_VERTICAL);
btnNewButton.setLayoutParams(BD);
relativeLayout.addView(btnNewButton);
setContentView(relativeLayout);
我希望它能帮助您