android中那个方法可以在java代码,而不是xml布局文件中让一个button居于屏幕中央,求解答
1个回答
展开全部
在代码中使用RelativeLayout实现即可,下面是详细步骤:
RelativeLayout mLayout = new RelativeLayout(this);
mLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
Button mButton = new Button(this);
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mButton.setLayoutParams(mParams);
mButton.setText("Center");
mLayout.addView(mButton);
setContentView(mLayout);
RelativeLayout mLayout = new RelativeLayout(this);
mLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
Button mButton = new Button(this);
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mButton.setLayoutParams(mParams);
mButton.setText("Center");
mLayout.addView(mButton);
setContentView(mLayout);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询