我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程将此视图的大小调整为“wrap_content”?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程将此视图的大小调整为“wrap_content”?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
使用以下代码设置您的属性:
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
为没有父级的视图设置。
您必须将 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);