简体   繁体   中英

Android Development - Gallery widget with text below

I'm new to android development and I've gotten stuck.

I want to implement a scrolling list of images with text below each image.

I am trying to use the Gallery widget. I've succeeded in creating a gallery which I can load from a list of images off the memory card, but I can't figure how to place text under each image.

Is this possible? Perhaps a gallery is not the right widget for this.

Here's my main window code:

package org.touchandgo.speak;

import java.io.File;
import java.io.FilenameFilter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class SpeakMainWindow extends LicenseCheckActivity  
{  
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;

    private Uri[] mUrls;  
    String[] mFiles=null;  


    void showToast(String msg) {
    AlertDialog ad = new AlertDialog.Builder(this).create();
    ad.setCancelable(false); // This blocks the 'BACK' button
    ad.setMessage(msg);
    ad.setButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    ad.show();

}

    public void onCreate(Bundle icicle)  
    {  
        super.onCreate(icicle);
        setContentView(R.layout.main);

        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            mExternalStorageAvailable = true;
            mExternalStorageWriteable = false;
        } else {
            mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

    if (mExternalStorageAvailable)
    {
        File images = new File ( "/sdcard/TouchAndGoSpeech");

        showToast (images.getPath());



        File[] imagelist = images.listFiles(new FilenameFilter()
        {  
            public boolean accept(File dir, String name)  
            {  
                return (name.endsWith(".jpg")||name.endsWith(".png"));  
            }  
        });

        mFiles = new String[imagelist.length];  

        for(int i= 0 ; i< imagelist.length; i++)  
        {  
            mFiles[i] = imagelist[i].getAbsolutePath();  
        }  
        mUrls = new Uri[mFiles.length];  

        for(int i=0; i < mFiles.length; i++)  
        {  
            mUrls[i] = Uri.parse(mFiles[i]);     
        }     

        Gallery g = (Gallery) findViewById(R.id.g_main);  
        g.setAdapter(new ImageAdapter(this));  
        g.setFadingEdgeLength(0);
        g.setHapticFeedbackEnabled(true);
        g.setSpacing(5);

    }
}  
public class ImageAdapter extends BaseAdapter
{  

    int mGalleryItemBackground;  
    public ImageAdapter(Context c)  {     
        mContext = c;     
    }  
    public int getCount(){  
        return mUrls.length;  
    }  
    public Object getItem(int position){  
        return position;  
    }  
    public long getItemId(int position) {  
        return position;  
    }  
    public View getView(int position, View convertView, ViewGroup parent){  
        ImageView i = new ImageView(mContext);  
        i.setTag(mUrls[position]);
        i.setImageURI(mUrls[position]);  
        i.setScaleType(ImageView.ScaleType.FIT_XY);  
        i.setLayoutParams(new Gallery.LayoutParams(48, 48));

        return i;  
    }     
    private Context mContext;  
}     
}    

您将需要从ImageAdapter修改getView()方法,以添加同时包含ImageViewTextViewLinearLayout ,您将用作标签,而不仅仅是ImageView。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM