添加描述到 Android Gallery -(图片和文字)
我正在开发我的新项目,但我不知道如何在我的图库下添加文本。
我有 3 张图片,如果我在图片 2 上滑动,我可以在图片 1 和 3 下看到正确的描述,但在所选图片(在本例中为数字 2)下没有出现描述。
这是我的代码:
public void onClick(View v, int scelta) {
// in base all'intero della scelta ho una voce del menu :)
if (scelta == 1) {
Intent i = new Intent(this, Parco.class);
startActivity(i);
} else if (scelta == 2) {
Intent j = new Intent(this, Servizi.class);
startActivity(j);
} else {
this.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?f=d&saddr=37.4,-121.9&daddr=Bellevue, WA&hl=en")));
}
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
private int itemBackground;
public ImageAdapter(Context c) {
ctx = c;
// Style
TypedArray a = obtainStyledAttributes(R.styleable.Gallery01);
itemBackground = a.getResourceId(
R.styleable.Gallery01_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int elemento, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
ImageView img = null;
// Riutilizziamo l'eventuale convertView.
if (arg1 == null) {
img = new ImageView(ctx);
} else {
img = (ImageView) arg1;
}
img.setImageResource(pics[elemento]);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new Gallery.LayoutParams(280, 210));
img.setBackgroundResource(itemBackground);
TextView tv = new TextView(ctx);
String titolo = "";
if (elemento == 0) {
titolo = "le Grotte";
} else if (elemento == 1) {
titolo = "il Parco";
} else if (elemento == 2) {
titolo = "i Servizi";
}
tv.setText(titolo);
tv.setGravity(Gravity.CENTER);
// Utilizzo l'AssetManager per cambiare il font
AssetManager assetManager = getResources().getAssets();
Typeface typeface = Typeface.createFromAsset(assetManager,
"fonts/CALIFR.TTF");
tv.setTypeface(typeface);
tv.setTextSize(50);
tv.setPadding(0, 0, 0, 40); // imposto il margine di bottom del
// testo
layout.addView(img);
layout.addView(tv);
return layout;
}
}
@Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
谢谢!!! 马可
I'm working on my new project but I can't figure out how to add a text under my Gallery.
I have 3 pictures, if I slide on picture 2 I can see under picture 1 and 3 the correct description but it doesn't appear the description under the selected picture (in this case the number 2).
This is my code:
public void onClick(View v, int scelta) {
// in base all'intero della scelta ho una voce del menu :)
if (scelta == 1) {
Intent i = new Intent(this, Parco.class);
startActivity(i);
} else if (scelta == 2) {
Intent j = new Intent(this, Servizi.class);
startActivity(j);
} else {
this.startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?f=d&saddr=37.4,-121.9&daddr=Bellevue, WA&hl=en")));
}
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
private int itemBackground;
public ImageAdapter(Context c) {
ctx = c;
// Style
TypedArray a = obtainStyledAttributes(R.styleable.Gallery01);
itemBackground = a.getResourceId(
R.styleable.Gallery01_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int elemento, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
ImageView img = null;
// Riutilizziamo l'eventuale convertView.
if (arg1 == null) {
img = new ImageView(ctx);
} else {
img = (ImageView) arg1;
}
img.setImageResource(pics[elemento]);
img.setScaleType(ImageView.ScaleType.FIT_XY);
img.setLayoutParams(new Gallery.LayoutParams(280, 210));
img.setBackgroundResource(itemBackground);
TextView tv = new TextView(ctx);
String titolo = "";
if (elemento == 0) {
titolo = "le Grotte";
} else if (elemento == 1) {
titolo = "il Parco";
} else if (elemento == 2) {
titolo = "i Servizi";
}
tv.setText(titolo);
tv.setGravity(Gravity.CENTER);
// Utilizzo l'AssetManager per cambiare il font
AssetManager assetManager = getResources().getAssets();
Typeface typeface = Typeface.createFromAsset(assetManager,
"fonts/CALIFR.TTF");
tv.setTypeface(typeface);
tv.setTextSize(50);
tv.setPadding(0, 0, 0, 40); // imposto il margine di bottom del
// testo
layout.addView(img);
layout.addView(tv);
return layout;
}
}
@Override
public View makeView() {
ImageView iView = new ImageView(this);
iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
iView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
iView.setBackgroundColor(0xFF000000);
return iView;
}
Thank you!!!
Marco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
tv.setTextColor(0xffffffff);
Use:
tv.setTextColor(0xffffffff);
TextView 类具有用于将 Drawable 放置在上方、下方等的属性
The TextView class has attributes to place a Drawable above, below, etc