如何在代码中声明将内容包装到动态按钮

时间:2011-08-01 13:07:15

标签: android

这是我的代码

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


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

如何将wrapcontent添加到此按钮?

3 个答案:

答案 0 :(得分:21)

使用此行(我使用FILL_PARENT演示)

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

答案 1 :(得分:2)

设置按钮

的换行内容
myButtonsetLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

或布局

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

答案 2 :(得分:0)

科特林

val button = Button(context)
button.layoutParams = ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)