2013-02-06 33 views
0

我是android新手。我想在特定的x和y位置在屏幕上顯示按鈕。 我有一個相對佈局,我把背景圖片放到相對佈局中,現在我想在特定的x和y位置上創建或繪製該圖片上的按鈕,並且X和Y位置由我通過XML給出,我有5個X和Y位置。我谷歌很多,但沒有找到任何相關的解決方案,每個人都說使用AbsoluteLayout我們可以做到這一點,但我不想使用AbsoluteLayout。如何繪製或顯示android中X和Y位置的按鈕?

請幫我一把。

+0

您可以嘗試設置每個按鈕的特定頂部和左側邊距值。 – Losiowaty

回答

0

;或

RelativeLayout yourRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout_1); 

RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
Params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
Params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
Params.leftMargin = 25; 
Params.topMargin = 25;  

Button newButton = new Button(this); 

yourRelativeLayout.addView(newButton, Params); 
+0

thanx調整您的按鈕,以便回答 – nishitpatel

0

我認爲你可以用AbsoluteLayout做到這一點。請參閱this link,它將解決您的問題。

+0

AbsolueLayout已被棄用,您可以更好地使用RelativeLayout並使用margin屬性 – banzai86

0

你可以用它來:RelativeLayout.LayoutParams

例如:

RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
Params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
Button button1 = new Button(this); 
button1.setLayoutParams(Params); 

Params= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
Params.addRule(RelativeLayout.RIGHT_OF, button1.getId()); 
Button button2 = new Button(this); 
button2.setLayoutParams(Params); 

我希望這個答案幫助你...