Android开发 - 图库小部件,下面是文本

时间:2011-06-18 00:12:15

标签: android android-layout gallery

我是Android开发的新手,我已经卡住了。

我想实现一个滚动的图像列表,每个图像下面都有文字。

我正在尝试使用Gallery小部件。我已经成功创建了一个可以从存储卡中的图像列表加载的图库,但我无法想象如何在每个图像下放置文本。

这可能吗?也许画廊不适合这个。

这是我的主窗口代码:

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

1 个答案:

答案 0 :(得分:1)

您需要修改getView()方法中的ImageAdapter方法,添加LinearLayout,其中包含您将用作ImageViewTextView的{​​{1}}标签,而不仅仅是ImageView。