在android中使用Toast中的数组中的Image

时间:2013-04-16 08:03:56

标签: android arrays image toast

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

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

数组:

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();

欢迎任何帮助,我这是一个菜鸟。欢呼声。

1 个答案:

答案 0 :(得分: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();