0

如何将图像添加到带有图像本身和文本的网格视图中。我尝试制作自定义图像适配器,但由于某些原因导致 imgView.setLayoutParams(new GridView.LayoutParams(85, 85)) 崩溃;

新问题如何从图像适配器中添加和删除...我需要的行为就像一张带有 a 的图片,我添加了带有文本 ab 的相同图片,所以应该有 2 张图片,然后我可以删除带有 a 的一张。 .

import java.util.ArrayList;

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

public class AlbumAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList albNames;

    public AlbumAdapter(Context c, ArrayList albNames) {

        mContext = c;
        this.albNames = albNames;
    }

    public int getCount() {

        return mThumbIds.length;
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
/*  public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 3);
        } 

        else {

            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    */

    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.album_image, null);
        } 

        else {

            view = convertView;
        }

        ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(50, 50));
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgView.setPadding(10, 10, 10, 3);
        TextView albNameView = (TextView) view.findViewById(R.id.icon_text);

        if (albNames.size() > 0) {

            for (int i = 0; i < albNames.size(); i++) {

                imgView.setBackgroundResource(R.drawable.folder);           
                albNameView.setText((String)albNames.get(i));

            }

        }




        return view;
    }


    // references to our images
    public Integer[] mThumbIds = { R.drawable.folder};

}
4

2 回答 2

0

尝试

imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));

编辑

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

public class AlbumAdapter extends BaseAdapter {

    private Context mContext;

    public AlbumAdapter(Context c) {

        mContext = c;
    }

    public int getCount() {

        return mThumbIds.length;
    }

    public Object getItem(int position) {

        return null;
    }

    public long getItemId(int position) {

        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
/*  public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;

        if (convertView == null) { // if it's not recycled, initialize some attributes

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(10, 10, 10, 3);
        } 

        else {

            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    */

    public View getView(int position, View convertView, ViewGroup parent) {

        View view;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.album_image, null);
        } 

        else {

            view = convertView;
        }

        ImageView imgView = (ImageView) view.findViewById(R.id.icon_image);
        imgView.setLayoutParams(new LinearLayout.LayoutParams(85, 85));
        imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imgView.setBackgroundResource(R.drawable.a);

        TextView albNameView = (TextView) view.findViewById(R.id.icon_text);
        albNameView.setText("as");

        return view;
    }


    // references to our images
    public Integer[] mThumbIds = { R.drawable.a };

}
于 2013-04-19T02:01:53.747 回答
0

'imgView' 是从 R.layout.album_image 膨胀的 ConvertView 的子级,它只不过是一个 LinearLayout。因此,在设置布局参数时,它需要是父 ViewGroup 类型。所以 LinearLayout.LayoutParams 工作正常。

假设您想设置“ConvertView”的大小,那么在这种情况下,它需要是 GridView.LayoutParams,因为“ConvertView”被添加到 GridView 的父级。

希望你明白了..!!

于 2013-04-19T02:12:40.273 回答