添加说明到Android图库 - (图片和文字)

时间:2010-10-06 14:25:26

标签: android text 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;
}

谢谢!!! 马可

2 个答案:

答案 0 :(得分:0)

TextView类具有在上面,下面等放置Drawable的属性

答案 1 :(得分:0)

使用:tv.setTextColor(0xffffffff);