编译GridView的android示例时出错

时间:2015-12-22 23:26:56

标签: android gridview layout layoutparams getview

我试图为GridView编写一个简单的适配器,完全来自GridView的google示例;这是我的getView功能。

imageView.setLayoutParams(new GridLayout.LayoutParams(120,120));

但是我为此行Error:(39, 39) error: no suitable constructor found for LayoutParams(int,int) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParam (Context,AttributeSet) is not applicable (actual argument int cannot be converted to Context by method invocation conversion) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(android.widget.GridLayout.LayoutParams) is not applicable (actual and formal argument lists differ in length) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(MarginLayoutParams) is not applicable (actual and formal argument lists differ in length) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(android.view.ViewGroup.LayoutParams) is not applicable (actual and formal argument lists differ in length) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams() is not applicable (actual and formal argument lists differ in length) constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(Spec,Spec) is not applicable (actual argument int cannot be converted to Spec by method invocation conversion)

收到了类似的错误
{{1}}

有没有解决方案?

2 个答案:

答案 0 :(得分:1)

检查以下代码:

import android.widget.GridLayout.LayoutParams;//import this statement

int dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics()); 
imageView.setLayoutParams(new LayoutParams(dp, dp));//120dp

答案 1 :(得分:-1)

尝试删除imageView.setLayoutParams(new GridLayout.LayoutParams(120,120));