添加描述到 Android Gallery -(图片和文字)

发布于 2024-09-26 12:07:07 字数 3133 浏览 6 评论 0原文

我正在开发我的新项目,但我不知道如何在我的图库下添加文本。

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-10-03 12:07:07

使用:tv.setTextColor(0xffffffff);

Use: tv.setTextColor(0xffffffff);

刘备忘录 2024-10-03 12:07:07

TextView 类具有用于将 Drawable 放置在上方、下方等的属性

The TextView class has attributes to place a Drawable above, below, etc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文