0

这是我的 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"
    };

}

如何在 ImageViews 下从 mThumbTxt 数组中添加图像描述(如在 TextView 中),而不会弄乱 GridView 结构?

4

2 回答 2

2

让你的观点是 aTextView而不是ImageView. 将图像设置为顶部可绘制对象:

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

这里唯一的缺点是可绘制对象将以其内部大小绘制。您可以创建一个 ClipDrawable 来解决这个问题。或者,使您的视图 aVerticalLayout包含 anImageView和 a TextView。那么,从 xml 加载它会更简单。

于 2012-06-03T15:40:47.270 回答
1

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

我 getView 的 ImageAdapter 方法:

 @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;
}
于 2012-06-03T15:46:00.097 回答