2015-03-30 37 views
0

我試圖做一個新的Relativelayout,然後我創建了一個按鈕,最後我將這個按鈕添加到佈局。 但「setContentView」有助於實現什麼?什麼「setContentView」幫助

下面是代碼

RelativeLayout wassimLayout = new RelativeLayout(this); 
Button redButton = new Button(this); 
wassimLayout.addView(redButton); 
setContentView(wassimLayout); 

我不知道,但我覺得它可能是設置按鈕高度,寬度等特性爲默認值。我如何防止這種情況?

感謝

+0

你能解釋你遇到的問題嗎? 'setContentView()'擴大了該活動的佈局 – codeMagic 2015-03-30 19:09:41

回答

1

由於要在代碼中創建,而不是在XML視圖,您需要創建並同時爲RelativeLayout的和按鈕設置的LayoutParams。

0

我不知道你寫的正確的,因爲連接的setContentView的Java文件與XML文件,並做到這一點,正確的做法是寫這

setContentView(R.layout.your_layout_name); 

這在Android是onCreate方法。