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”之类的重力和文本大小?

4

4 回答 4

2

您可以在此处查找方法http://developer.android.com/reference/android/widget/Button.html

ammian_martsellin_1.setGravity(int)
ammian_martsellin_1.setTextSize(int,float)
于 2013-07-02T15:09:15.770 回答
0

您应该能够使用 setGravity 来设置重力。尝试调用 ammian_martsellin_1.setGravity(GRAVITY_VALUE);

于 2013-07-02T15:06:19.427 回答
0

你可以试试这个:

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

我不确定 instanceOf 的语法是否正是这种方式,但我相信你会修复它以防万一它是错误的。

于 2013-07-02T15:23:09.523 回答
0

LinearLayout.LayoutParams主要是只读的,原因是,它主要是从AttributeSet从 XML 资源获取的实现中创建的。

您必须调用setGravity()所有视图对象。

于 2013-07-02T15:12:11.543 回答