1

动态按钮代码:

final Button button = new Button(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);
Resources resources = SecondActivity.this.getResources();  
Drawable drawable = resources.getDrawable(R.drawable.buttonpg);
button.setBackgroundDrawable(drawable);
button.setText(String.valueOf(i));
button.setText(" " + nameArray[i]);
button.setId(i);
button.setWidth(180);
button.setHeight(60);

button.setTextColor(Color.rgb(255,69,0));
button.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
button.setGravity(Gravity.CENTER);

如何为此按钮添加 Margintop ....

谢谢你的帮助....

4

3 回答 3

0

使用代码

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);button.setLayoutParams(params);
于 2013-03-01T11:17:51.017 回答
0
final Button button = new Button(this);
LayoutParams params = new LayoutParams(
    LayoutParams.WRAP_CONTENT,      
    LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
button.setLayoutParams(params);
于 2013-03-01T11:18:00.520 回答
0

您可以创建一个实例LinearLayout.LayoutParams,为属性赋值topMargin并将该实例设置为按钮。希望有所帮助。

于 2013-03-01T11:19:01.040 回答