如何在填充有 ImageView 的 GridView 中添加 TextView

How to add TextView inside a GridView filled with ImageView

我有一个 GridView,我可以在其中直接使用 ImageView 添加图像(我没有为图像使用 XML)。现在,我想使用 TextView 在每个单元格的底部添加图像名称。我该怎么做?提前致谢。

自定义适配器Class:

public class GridViewImageAdapter extends BaseAdapter {

    private Activity _activity;
    private ArrayList<String> _filePaths = new ArrayList<String>();
    private int imageWidth;
    private GridView imageCarrete;

    public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths,
                            int imageWidth, GridView imageCarrete) {
        this._activity = activity;
        this._filePaths = filePaths;
        this.imageWidth = imageWidth;
        this.imageCarrete = imageCarrete;
    }

    @Override
    public int getCount() {
        return this._filePaths.size();
    }

    @Override
    public Object getItem(int position) {
        return this._filePaths.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(_activity);
        } else {
            imageView = (ImageView) convertView;
        }

        // get screen dimensions
        Bitmap image = decodeFile(_filePaths.get(position), imageWidth,
            imageWidth);

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
            imageWidth));
        imageView.setImageBitmap(image);

        // Listener del GridView
        imageCarrete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Intent i = new Intent(_activity, FullScreenViewActivity.class);
                i.putExtra("position", position);
                _activity.startActivityForResult(i, 1234);
            }
        });

        return imageView;
    }

    /*
     * Resizing image size
     */
    public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
        try {

            File f = new File(filePath);

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f), null, o);

            final int REQUIRED_WIDTH = WIDTH;
            final int REQUIRED_HIGHT = HIGHT;
            int scale = 1;
            while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                    && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                scale *= 2;

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void remove(String path){
        _filePaths.remove(path);
    }

}

网格视图XML

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:gravity="center"
    android:stretchMode="columnWidth"
    android:background="#000000">
</GridView>

在您的 getView() 方法中,将您的 ImageView 替换为带有可绘制对象的 TextView

TextView tv = new TextView(context);
 tv.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

您将提供 drawable 到顶部,0 到其余部分,即:

tv.setCompoundDrawablesWithIntrinsicBounds(0, yourDrawable, 0, 0);

只需像在 getView() 方法中创建 Imagview 一样创建一个 LinearLayout...用 [=16] 添加 ImageViewTextView =] 方法....看看..

@Override
public View getView(int position, View convertView, ViewGroup parent) {
      LinearLayout _ll;
      if (convertView == null) {            
        _ll = new LinearLayout(mContext);
        _ll.setOrientation(LinearLayout.VERTICAL);

        ImageView img  = new ImageView(mContext);
        img.setLayoutParams(new LinearLayout.LayoutParams(100, 100));   // your image size
        img.setBackgroundResource(images[position]);                    // here pass your array list position
        _ll.addView(img);

        TextView text = new TextView(mContext);
        text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        text.setText("dsaf");
        text.setGravity(Gravity.CENTER);
        _ll.addView(text);            
      } else {
          _ll = (LinearLayout) convertView;
      }
  return _ll;
}

如果您想将文本覆盖在图像上..

,您也可以使用RelativeLayout