2011-08-01 37 views
8

這裏是我的代碼如何包裝內容申報動態按鈕代碼

Button myButton = new Button(this); 
myButton.setText("Press Me"); 
myButton.setTextColor(Color.WHITE); 


LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1); 
layout.addView(myButton); 

如何添加wrapcontent這個按鈕?

回答

18

使用這條線(我用FILL_PARENT證明)

layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 
+0

感謝myButton.setLayoutParams包裹內容(新的LayoutParams (LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));爲我工作 – Sando

+0

hehe是啊你需要它爲我的按鈕!我認爲佈局:)同樣的事情 –

2

設置按鈕

myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

或佈局

layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));