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

时间:2016-02-02 16:25:17

标签: android gridview textview imageview baseadapter

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

自定义适配器类:

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);
    }

}

GridView 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>

2 个答案:

答案 0 :(得分:2)

ngDialog.open({ template: 'template1.html',controller: 'test', className: 'ngdialog-theme-default', data:$scope.obj}); 方法中,将您的getView()替换为ImageView并使用drawable:

TextView

您将把drawable提供给顶部,TextView tv = new TextView(context); tv.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom); 提供给其余部分,即:

0

答案 1 :(得分:0)

LinearLayout方法中创建Imagview时,只需创建一个getView() ... ImageView添加TextViewaddView() @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

如果您想将文字覆盖在图片上,也可以使用* { box-sizing: border-box; }