2013-07-02 36 views
0

在我的程序中有超過2000個以編程方式創建的按鈕。他們是這樣的:如何添加其他參數以編程方式創建按鈕?

//Parameters of buttons 
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

//Create a button based on... 
if (bla-bla) 
{ 
Button ammian_martsellin_1=new Button(this); ammian_martsellin_1.setText(R.string.ammian_martsellin_1); llPreViewList.addView(ammian_martsellin_1, lParams); 
Button ammian_martsellin_2=new Button(this); ammian_martsellin_2.setText(R.string.ammian_martsellin_2); llPreViewList.addView(ammian_martsellin_2, lParams); 
} 
//And a lot of these groups of buttons. 

如何添加額外的參數,這些按鈕,如重力和文字大小,如「lParams」?

+0

但我有很多的按鈕,我將不得不所有的按鈕來追加新的參數? – user2531700

回答

0

您應該可以使用setGravity來設置重力。嘗試調用ammian_martsellin_1.setGravity(GRAVITY_VALUE);

0

LinearLayout.LayoutParams大多是從XML資源得到AttributeSet實現只讀,原因是,它主要是創建。

您必須在所有視圖對象上調用setGravity()

0

你可以試試這個:

for(int i=0;i<ViewGroup.getChildCount();i++){ 
    if(ViewGroup.getChildAt(i).instanceOf(Button)){ 
//Do stuff 
    } 
} 

林不知道如果}這種語法是準確的方式,但是我相信你會解決這個問題的情況下,這是錯誤的。

相關問題