Adding image to Toast?

后端 未结 8 1922
梦毁少年i
梦毁少年i 2020-12-02 05:53

Is it possible to programmatically add an image to a toast popup?

8条回答
  •  独厮守ぢ
    2020-12-02 06:33

    Knickedi's solution is good, but if you only need an icon next to the text you can make use of the fact that the Toast has a pre-defined TextView with the same ID and set the icon on the TextView:

    Toast toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
    TextView tv = (TextView) toast.getView().findViewById(android.R.id.message);
    if (null!=tv) {
        tv.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
        tv.setCompoundDrawablePadding(context.getResources().getDimensionPixelSize(R.dimen.padding_toast));
    

提交回复
热议问题