2016-03-07 30 views

回答

0

,不能设置为LayoutParams没有视图家长。

+0

这是否意味着我没有任何方法来更改视图大小,而不是在布局中包装此视图? –

+0

是的,没有布局你的按钮无法显示。 –

+0

我的按钮可以显示,它具有和高度等于屏幕尺寸,但似乎android不会隐式创建一些布局,因为getParrent()或getLayoutParams()返回null –

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); 
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); 
+0

它似乎没有工作 –

相关问题