【问题标题】:Android Development - Gallery widget with text belowAndroid 开发 - Gallery 小部件,下方有文字
【发布时间】:2011-06-18 00:12:15
【问题描述】:

我是 android 开发的新手,我被卡住了。

我想实现一个图像滚动列表,每个图像下方都有文本。

我正在尝试使用图库小部件。我已经成功创建了一个画廊,我可以从存储卡上的图像列表中加载它,但我不知道如何在每张图像下放置文本。

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

这是我的主窗口代码:

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

【问题讨论】:

    标签: android android-layout gallery


    【解决方案1】:

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

    【讨论】:

    • 感谢 Aleadam,我现在可以显示图像并且图像的文本出现在它的右侧,这实际上对我的目的来说还不错,但我希望文本出现在下面图片。我认为我没有完全掌握 getView 的概念。
    • 这是我的代码: public View getView(int position, View convertView, ViewGroup parent) { LinearLayout ll = new LinearLayout(mContext); ImageView i = new ImageView(ll.getContext()); i.setTag(mUrls[位置]); i.setImageURI(mUrls[位置]); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(48, 48)); TextView tv = new TextView(ll.getContext()); tv.setTag(mFiles[位置]); tv.setText(mFiles[位置]); tv.setLayoutParams(new Gallery.LayoutParams(48, 48)); ll.addView(tv);返回 ll; }
    • 您只需将方向设置为垂直:ll.setOrientation(LinearLayout.VERTICAL);。见:developer.android.com/reference/android/widget/…
    猜你喜欢
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多