要导入什么来解决“LayoutParams无法解析为变量”?

时间:2014-02-05 14:08:04

标签: android-layout layoutparams resolve

我在一行代码上遇到错误LayoutParams cannot be resolved to a variable,如下所示tv.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.1f));
当我按ctrl + shift + o导入必要的pacakges时,我会得到很多版本的软件包作为选项。我很困惑哪一个应该做这个工作。 here是我得到的选项

导入import android.app.ActionBar.LayoutParams;

仍然出现错误

The constructor AbsListView.LayoutParams(int, int, float) is undefined

2 个答案:

答案 0 :(得分:1)

可以通过导入[任何布局] .LayoutParams来解决 例如。 import android.widget.GridLayout.LayoutParams; 虽然我不清楚选择导入哪个包的标准是什么。 解决问题

the constructor AbsListView.LayoutParams(int, int, float) is undefined

确保第三个参数也是“int” 它应该是:

LayoutParams cannot be resolved to a variable on a line of code which is as follows tv.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1));

答案 1 :(得分:1)

只需添加以下语句即可将LayoutParams与LinearLayout一起使用:

import android.widget.LinearLayout;

然后您可以轻松编写以下代码:

LinearLayout ll1;    
LayoutParams params1;

/*Creating a linear layout*/

ll1=new LinearLayout(this);
ll1.setOrientation(LinearLayout.VERTICAL);
/*set the width and height of the linear layout*/

params1=new LayoutParams(LayoutParams.MATCH_PARENT,    LayoutParams.MATCH_PARENT);
setContentView(ll1,params1);