我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程方式将此视图调整为“wrap_content”?是否可以在没有parrent的情况下设置wrapContent属性?
Button button = new Button(getContext());
button.setText("Some text");
setContentView(button);
我有一个简单的按钮视图,并将其设置为内容视图。是否可以通过编程方式将此视图调整为“wrap_content”?是否可以在没有parrent的情况下设置wrapContent属性?
Button button = new Button(getContext());
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);
设置使用下面的代码你的属性:
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);
它似乎没有工作 –
这是否意味着我没有任何方法来更改视图大小,而不是在布局中包装此视图? –
是的,没有布局你的按钮无法显示。 –
我的按钮可以显示,它具有和高度等于屏幕尺寸,但似乎android不会隐式创建一些布局,因为getParrent()或getLayoutParams()返回null –