2013-07-02 34 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 
    } 
} 

林不知道如果}这种语法是准确的方式,但是我相信你会解决这个问题的情况下,这是错误的。

相关问题