GridView中ImageView下的TextView描述

时间:2012-06-03 15:34:23

标签: android gridview

这是我的ImageAdapter类:

package com.javacodegeeks.android.googlemaps;

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

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(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(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

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

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.muze, R.drawable.anit, R.drawable.ibadethane, R.drawable.hepsimenu
    };

    private String[] mThumbTxt = {
            "Müzeler", "Anıtlar", "İbadethaneler", "Hepsi"
    };

}

如何在不影响GridView结构的情况下从ImageViews中的mThumbTxt数组添加图像描述(如在TextView中)?

2 个答案:

答案 0 :(得分:2)

让您的观点为TextView,而不是ImageView。将图像设置为顶部可绘制:

textView.setCompoundDrawablesWithIntrinsicBounds(0, mThunbIds[position], 0, 0);
textView.setText(mThumbTxt[position]);

这里唯一的缺点是绘图将以其内在的大小绘制。您可以创建一个ClipDrawable来修复它。或者,将您的观看设为VerticalLayout,其中包含ImageViewTextView。那么,从xml加载它会更简单。

答案 1 :(得分:1)

简单的方法为gridView添加一个布局:

gridView.xml:

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/relaGrid"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
           <ImageView android:id="@+id/chooseImage"
                       android:src="@drawable/ph1"
                        android:layout_width="85dp"
                        android:layout_height="85dp"
                        >
                        </ImageView>
    <TextView android:id="@+id/chooseText"
                      android:layout_height="wrap_content"
                      android:layout_width="85dp"
                      android:text="TextView"
                      android:layout_below="@+id/chooseImage"
                      android:gravity="center"
                      android:singleLine="true"
                      android:ellipsize="marquee"
                      android:marqueeRepeatLimit="marquee_forever"
                      ></TextView>

</RelativeLayout>

我是ImageAdapter的getView方法:

 @Override
public View getView(int position, View convertView, ViewGroup arg2) {
View view = View.inflate(mContext, R.layout.relagrid, null);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relaGrid);

ImageView image = (ImageView) rl.findViewById(R.id.chooseImage);
TextView text = (TextView) rl.findViewById(R.id.chooseText);       
image.setImageResource(mThumbIds[position]);
text.setText(mThumbTxt[position]);
return rl;
}