1

我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程将此视图的大小调整为“wrap_content”?

Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
4

3 回答 3

1

使用以下代码设置您的属性:

Button button = new Button(getContext());

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);

button.setText("Some text");
setContentView(button);
于 2016-03-07T12:17:31.940 回答
0

您不能LayoutParams为没有父级的视图设置。

于 2016-03-07T12:28:28.337 回答
0

您必须将 LinearLayout.LayoutParams 与以下内容一起使用:

Button button = new Button(getContext());

LinearLayout.LayoutParams params = new   LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
params.weight = 1.0f;
button.setText("Some text");
setContentView(button);
于 2016-03-07T12:16:50.387 回答