我一直在SetLayoutParams上出错 - 无法从静态上下文中引用?

时间:2017-03-16 13:57:18

标签: android-studio android-imageview android-context layoutparams

我已经从android studio粘贴了下面的全班。我试图使用图像视图和GridView创建一个库。

package com.example.sc.loginappexercise;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

/**
 * Created by sc on 16/03/2017.
 */

public class ImageAdapter extends BaseAdapter {

    public Context context;
    public int[] images = {
            R.drawable.gallery1, R.drawable.gallery2,
            R.drawable.gallery3, R.drawable.gallery4,
            R.drawable.gallery5, R.drawable.gallery6,
            R.drawable.gallery7, R.drawable.gallery8,
            R.drawable.gallery9, R.drawable.gallery10,


    };

    public ImageAdapter(Context c){
        context = c;
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object getItem(int position) {
        return images [position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
}

**在它说setLayoutParams的地方 - 它保持红色并抛出错误。无法从静态上下文中引用? **

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(images[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        ImageView.setLayoutParams(new GridView.LayoutParams(240, 240));
        return imageView;
    }
}

1 个答案:

答案 0 :(得分:1)

ImageView.setLayoutParams(new GridView.LayoutParams(240, 240)); => imageView.setLayoutParams(new GridView.LayoutParams(240, 240));

我=>我

仅此而已!