Android Gallery - 使用 TextView 和 ImageView 不起作用?
我有一个带有 ImageViews 的图库,我想让每个图像视图下都显示一个文本。我的代码不起作用,我不知道为什么。我在 Android 编程方面不是那么先进,所以编辑后的代码响应将真正有帮助。当我从第一个图像快速滚动到最后一个图像时,我也遇到内存错误。
PS:我尝试过很多 stackoverflow 类似的主题,但没有一个对我有用。
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.text,
R.drawable.numbers,
R.drawable.random,
R.drawable.programming,
R.drawable.left_hand,
R.drawable.right_hand,
R.drawable.maths,
R.drawable.capitals,
R.drawable.emoticons,
R.drawable.spaces,
R.drawable.fast,
R.drawable.ultimate,
};
private String[] scoreIndex={"1.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56"};
private int j=0;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new Gallery.LayoutParams(250, 200));
ImageView i = null;
// Riutilizziamo l'eventuale convertView.
if (arg1 == null) {
i = new ImageView(mContext);
} else {
i = (ImageView) arg1;
}
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(250, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
TextView tv=new TextView(mContext);
tv.setTag(scoreIndex[position]);
tv.setText(scoreIndex[position]);
tv.setTextColor(0x000000);
tv.setTextSize(50);
tv.setPadding(0, 0, 0, 40);
tv.setGravity(Gravity.CENTER);
layout.addView(i);
layout.addView(tv);
return layout;
}
}
I have a Gallery with ImageViews and I want to make a text appear under each of them. My code doesn't work and I don't know why. I am not so advanced in Android programming so an edited Code response will be truly helpful. I also have a memory error when I fast scroll from the first image to the last one.
P.S: I have tried many stackoverflow similar topics but none of them has worked for me.
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.text,
R.drawable.numbers,
R.drawable.random,
R.drawable.programming,
R.drawable.left_hand,
R.drawable.right_hand,
R.drawable.maths,
R.drawable.capitals,
R.drawable.emoticons,
R.drawable.spaces,
R.drawable.fast,
R.drawable.ultimate,
};
private String[] scoreIndex={"1.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56"};
private int j=0;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new Gallery.LayoutParams(250, 200));
ImageView i = null;
// Riutilizziamo l'eventuale convertView.
if (arg1 == null) {
i = new ImageView(mContext);
} else {
i = (ImageView) arg1;
}
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(250, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
TextView tv=new TextView(mContext);
tv.setTag(scoreIndex[position]);
tv.setText(scoreIndex[position]);
tv.setTextColor(0x000000);
tv.setTextSize(50);
tv.setPadding(0, 0, 0, 40);
tv.setGravity(Gravity.CENTER);
layout.addView(i);
layout.addView(tv);
return layout;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了解决您的内存问题,请尝试重用一些视图,而不是每次都创建它们...... getView() 的第二个参数是一个可以转换的视图。您应该检查它是否为非空(可以为空)并且它是正确的类型(仅当您的列表包含一组异构视图类型时才需要;从这个代码示例来看,我认为这不适用于您)。然后,您可以清除该视图的当前内容并在其中设置图像/文本,而不是膨胀新视图。当您的列表变大时,这应该可以节省大量时间/内存。
要解决布局问题:不要完全通过代码创建视图,而是在画廊项目自己的布局 xml 文件中定义 LinearLayout。然后您可以使用视图充气机对其进行充气。您应该能够使用可视化编辑器来确保布局符合您的要求。
这是我的意思的一个示例(包含两个建议):
To address your memory issue, try reusing some of your views rather than creating them each time... the 2nd argument to getView() is a view that can be converted. You should check that it is non-null (it can be null) and that it's the right type (only needed if your list contains a heterogeneous set of view types; from this code sample I don't think this applies to you). Then you can clear out the curent contents of that view and set the image/text in those rather than inflating a new view. This should save a significant amount of time/memory as your list gets large.
To address your layout issues: rather than totally creating the view via code, define the LinearLayout for a gallery item in its own layout xml file. You can then use the view inflator to inflate that. You should be able to use the visual editor to make sure the layout is as you want it that way.
Here's a sample of what I mean (with both suggestions):