开发者

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


You will need to modify your getView() method from your ImageAdapter to add a LinearLayout containing both the ImageView and the TextView you will use as a label, instead of just the ImageView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜