【发布时间】:2016-03-07 12:15:41
【问题描述】:
我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程方式将此视图的大小调整为“wrap_content”?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
【问题讨论】:
标签: android view android-wrap-content
我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程方式将此视图的大小调整为“wrap_content”?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
【问题讨论】:
标签: android view android-wrap-content
使用以下代码设置您的属性:
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);
【讨论】:
您不能为没有父视图的视图设置LayoutParams。
【讨论】:
WindowManager 管理的Window。阅读更多:stackoverflow.com/a/22442702/1798031
你必须使用 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);
【讨论】: