2012-07-04 79 views
2

告訴我如何以編程方式使用線性佈局將位置分配給android中的按鈕。默認情況下,它將顯示屏幕的最左邊位置。另外我想要使用線性佈局,請不要使用相對佈局。下面是我的代碼以編程方式將位置分配給android中的按鈕

buttons_and_edittext=new LinearLayout(HelloAugmentedWorldActivity.this); 
buttons_and_edittext = (LinearLayout)findViewById(R.id.linearLayout1); 
buttons_and_edittextParameters = new LinearLayout.LayoutParams(120, 150); 

button3 = new Button(this); 
button3.setText("log"); 

buttons_and_edittext.addView(button3,  
buttons_and_edittextParameters); 

任何幫助將不勝感激感謝

+0

參考前面的問題http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – Aerrow

+0

感謝ü非常。我只需要使用buttons_and_edittextParameters.setMargins(30,20,30,0); – Joyson

+0

請你建議我一個網站wer我可以得到很好的教程使用相對佈局我總結什麼感覺不安使用相對佈局,因爲我是新的android。 – Joyson

回答

3

請參考以下鏈接在固定位置添加按鈕進入相對佈局。

Android User Interface Design: Relative Layout

,並使用下面的代碼也爲這一點。

Button but1 = new Button(this); 
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
but1.setLayoutParams(params2); 
but1.setText("Press Here!"); 
// give the button an id that we know 
but1.setId(1001); 
layout1.addView(but1); 
相關問題