是否有可能在没有parrent的情况下在视图上设置wrapContent属性?

时间:2016-03-07 12:15:42

标签: android view android-wrap-content

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

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分:0)

您无法为没有父级的视图设置LayoutParams

相关问题