Android开发-图库小部件,文本如下
我是android开发的新手,我被卡住了 我想实现一个滚动图像列表,每个图像下面都有文本 我正在尝试使用Gallery小部件。我成功地创建了一个图库,可以从存储卡上的图像列表中加载,但我不知道如何在每个图像下放置文本 这可能吗?也许gallery不是适合这个应用的小部件 这是我的主窗口代码:Android开发-图库小部件,文本如下,android,android-layout,gallery,Android,Android Layout,Gallery,我是android开发的新手,我被卡住了 我想实现一个滚动图像列表,每个图像下面都有文本 我正在尝试使用Gallery小部件。我成功地创建了一个图库,可以从存储卡上的图像列表中加载,但我不知道如何在每个图像下放置文本 这可能吗?也许gallery不是适合这个应用的小部件 这是我的主窗口代码: package org.touchandgo.speak; import java.io.File; import java.io.FilenameFilter; import android.app.
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;
}
}
package org.touchandgo.speak;
导入java.io.File;
导入java.io.FilenameFilter;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.ContextWrapper;
导入android.content.DialogInterface;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.Gallery;
导入android.widget.ImageView;
公共类SpeakMain窗口扩展LicenseCheckActivity
{
布尔值mExternalStorageAvailable=false;
布尔值mExternalStorageWriteable=false;
私有Uri[]mUrls;
字符串[]mFiles=null;
void showtoos(字符串msg){
AlertDialog ad=新建AlertDialog.Builder(this.create();
ad.setCancelable(false);//这会阻止“后退”按钮
ad.setMessage(msg);
ad.setButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
广告秀();
}
创建公共空间(捆绑冰柱)
{
超级冰柱;
setContentView(R.layout.main);
String state=Environment.getExternalStorageState();
if(环境、介质、安装等于(状态)){
mExternalStorageAvailable=mExternalStorageWriteable=true;
}else if(Environment.MEDIA\u MOUNTED\u READ\u ONLY.equals(state)){
mExternalStorageAvailable=true;
mExternalStorageWriteable=false;
}否则{
mExternalStorageAvailable=mExternalStorageWriteable=false;
}
如果(mExternalStorageAvailable)
{
文件图像=新文件(“/sdcard/TouchAndGoSpeech”);
showtoos(images.getPath());
File[]imagelist=images.listFiles(新的FilenameFilter()
{
公共布尔接受(文件目录,字符串名称)
{
return(name.endsWith(“.jpg”)| | name.endsWith(“.png”);
}
});
mFiles=新字符串[imagelist.length];
对于(int i=0;i
您需要从ImageAdapter
修改getView()
方法,以添加一个LinearLayout
,其中包含将用作标签的ImageView
和TextView
,而不仅仅是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[position]);I.setImageURI(mUrls[position]);I.setScaleType(ImageView.ScaleType.FIT_XY);i.setLayoutParams(new Gallery.LayoutParams(48,48));TextView tv=new TextView(ll.getContext());tv.setTag(mFiles[position]);tv.setText(mFiles[position]);tv.setLayoutParams(new Gallery.LayoutParams(48,48));ll.addView(tv);返回ll;}只需将方向设置为垂直:ll.setOrientation(LinearLayout.vertical)代码>。见: