我正在使用Eclipse爲Android創建應用程序。我知道你在XML
文件中設置了諸如居中按鈕或圖像等屬性,但是如何在Java中執行此操作?居中按鈕或圖像
Q
居中按鈕或圖像
1
A
回答
0
很難說,不知道你有什麼。但是,如果您查看文檔,則每個xml attribute
在Java中都有相關的方法。例如,Look at the View Docs,您可以看到一個列表attributes
及其相應的Java方法和實際內容的描述。你應該能夠使用它來讓你開始,然後你可以用你嘗試過的代碼,你正在遇到的確切問題以及你可能得到的任何錯誤信息來提問。祝你好運!
0
使用代碼來居中你的東西,而不是XML
文件中的屬性。
中心按鈕:
使用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);
}
0
你試過:
Button button = new Button(getApplicationContext());
button.setLayoutParams(newLayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
button.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
相關問題
- 1. 切換按鈕圖像不居中
- 2. 圖像疊加垂直居中按鈕
- 3. c#按鈕上的圖像和文本,居中在按鈕中?
- 4. 垂直居中文本輸入按鈕或帶圖像的按鈕
- 5. iPhone:按下按鈕 - >居中地圖
- 6. 可調整大小的圖像按鈕或asp.net中的按鈕
- 7. 試圖居中菜單按鈕
- 8. 位圖背景上的居中按鈕
- 9. Extjs 3.4 - 按鈕圖標不顯示或不居中
- 10. 在流體圖像上保持按鈕居中
- 11. 用圖像和文本居中作爲例子按鈕
- 12. 安卓工具欄圖像居中與導航抽屜按鈕
- 13. 居中DIV中的按鈕
- 14. div中的居中按鈕
- 15. Android:圖像按鈕或按鈕突出顯示效果時按
- 16. 按鈕VS JAVA中的圖像按鈕
- 17. 如何居中或縮放zoomButton圖像?
- 18. 如何在UISearchbar中更改清除按鈕圖像(x按鈕)或至少清除按鈕圖像的顏色?
- 19. 按鈕圖像
- 20. 使用css居中按鈕
- 21. 如何居中按鈕框?
- 22. 居中按鈕的形式
- 23. 居中html按鈕鏈接
- 24. 居中jquery-ui按鈕
- 25. 我不能居中按鈕
- 26. 居中按鈕文本
- 27. 無法居中按鈕
- 28. 我如何居中按鈕?
- 29. 居中提交按鈕div
- 30. Bootstrap,表,居中按鈕