我正在使用Eclipse为Android创建一个应用程序。我知道您在XML文件中设置了一些属性,比如使按钮或图像居中,但是在Java语言中如何设置呢?
发布于 2013-04-04 06:46:28
如果不知道你拥有什么,就很难说出口。但是,如果您浏览一下文档,就会发现每个xml attribute都有一个相关的方法。例如,Look at the View Docs,您可以看到attributes的列表及其对应的Java方法,以及对其实际功能的描述。你应该能够使用它来开始你的工作,然后你可以用你已经尝试过的代码,你遇到的准确的问题,以及你可能得到的任何错误消息来询问问题。祝好运!
发布于 2013-04-04 06:49:08
在XML文件中使用代码居中,而不是属性居中。
居中a按钮:
使用FlowLayout。这将使按钮在容器中水平居中,而不是垂直居中。
JButton button = new JButton("Click Me!");
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(button);如果你希望它在两个方向居中,你可以使用BoxLayout。
JButton button = new JButton("Click Me!");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createVerticalGlue());
panel.add(button);
panel.add(Box.createVerticalGlue());中心图片:
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int xCenter = (this.getWidth() - image.getWidth()) / 2;
int yCenter = (this.getHeight() - image.getHeight()) / 2;
g2d.drawImage(image, xCenter, yCenter, null);
}发布于 2013-04-04 06:53:57
您是否尝试过:
Button button = new Button(getApplicationContext());
button.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
button.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);https://stackoverflow.com/questions/15799528
复制相似问题