0

我正在制作一个 android 应用程序,让用户玩饮酒游戏国王。我在一个随机选择的数组“卡片”中拥有所有@drawable 图像。如果不创建布局或将 toast 设置为仅一个图像,我无法弄清楚如何在 toast 中显示图像。

这是我到目前为止所得到的...

数组:

final int [] cards = {R.drawable.twoofdiamonds, R.drawable.threeofdiamonds, R.drawable.fourofdiamonds, R.drawable.fiveofdiamonds, R.drawable.sixofdiamonds,
          R.drawable.sevenofdiamonds, R.drawable.eightofdiamonds, R.drawable.nineofdiamonds, R.drawable.tenofdiamonds, R.drawable.jofdiamonds, R.drawable.qofdiamonds,
          R.drawable.kofdiamonds, R.drawable.aceofdiamonds, R.drawable.twoofclubs, R.drawable.threeofclubs, R.drawable.fourofclubs, R.drawable.fiveofclubs,
          R.drawable.sixofclubs, R.drawable.sevenofclubs, R.drawable.eightofclubs, R.drawable.nineofclubs, R.drawable.tenofclubs, R.drawable.jofclubs,
          R.drawable.qofclubs, R.drawable.kofclubs, R.drawable.aceofclubs, R.drawable.twoofhearts, R.drawable.threeofhearts, R.drawable.fourofhearts,
          R.drawable.fiveofhearts, R.drawable.sixofhearts, R.drawable.sevenofhearts, R.drawable.eightofhearts, R.drawable.nineofhearts, R.drawable.tenofhearts, R.drawable.jofhearts,
          R.drawable.qofhearts, R.drawable.kofhearts, R.drawable.aceofhearts, R.drawable.twoofspades, R.drawable.threeofspades, R.drawable.fourofspades,
          R.drawable.fiveofspades, R.drawable.sixofspades, R.drawable.sevenofspades, R.drawable.eightofspades, R.drawable.ninefspades, R.drawable.tenofspades,
          R.drawable.jofspades, R.drawable.qofspades, R.drawable.kofspades, R.drawable.aceofspades};

我在 Image toast 上所做的尝试:

Toast ImageToast = new Toast(getBaseContext());
        LinearLayout toastLayout = new LinearLayout(getBaseContext());
        toastLayout.setOrientation(LinearLayout.HORIZONTAL);
        ImageView image = new ImageView(getBaseContext());
        TextView text = new TextView(getBaseContext());

        image.setImageResource(R.drawable.cards[randomSequence[cardCount]]); //error here

        text.setText("Hello!");
        toastLayout.addView(image);
        toastLayout.addView(text);
        ImageToast.setView(toastLayout);
        ImageToast.setDuration(Toast.LENGTH_LONG);
        ImageToast.show();

欢迎任何帮助,我是这方面的菜鸟。干杯。

4

1 回答 1

0

创建扩展 Toast 的自定义 Toast 类。

public class CustomToast extends Toast {
    public CustomToast(Context context) {
        super(context);
    }
    public CustomToast(Context context, int resourceId) {
            super(context);
            ImageView img = new ImageView(context);
            img.setImageResource(resourceId);
            setView(img);
    }
}

您可以使用它来调用它

new CustomToast(getApplicationContext(), cards[randomSequence[cardCount]]).show();
于 2013-04-16T08:38:14.503 回答