在 Android 中向画廊的每个图像项目添加文本

Adding text to each image item of a Gallery in Android

我有一个图片库,我想在每张图片下方显示最后修改的数据,但 TextView 中没有显示任何内容(但在 ImageView 中显示)。这是我的适配器。

public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {

    private Context context;
    private Drawable[] pics;
    private String[] filePaths;

    public ImageAdapter(Context context, Drawable[] pics, String[] filePaths) {
        this.context=context;
        this.pics =pics;
        this.filePaths=filePaths;
    }

    @Override
    public int getCount() {
        return pics.length;
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v=view;
        ImageView imageView;
        TextView textView;


        if(v==null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.gallery_item, null);
        }

        imageView=(ImageView)v.findViewById(R.id.galleryImageView);
        textView=(TextView)v.findViewById(R.id.galleryTextView);

        imageView.setLayoutParams(new Gallery.LayoutParams(115, 100));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);

        File f=new File(filePaths[i]);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd/MM/yyyy");
        Date lastModified=new Date(f.lastModified());

        imageView.setImageDrawable(pics[i]);
        textView.setText(simpleDateFormat.format(lastModified));

        return imageView;
    }

}

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/galleryImageView" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/galleryTextView"
        android:textColor="#000"/>
</LinearLayout>

正如我所说,显示了图片,但下面没有文字。

您返回的是 ImageView 而不是 View(Layout)。

使用此代码

return v;

Return

return v;

而不是

return imageView;