繁体   English   中英

Android开发-带有以下文本的Gallery小部件

[英]Android Development - Gallery widget with text below

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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