Adding image to Toast?

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

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

相关标签:
8条回答
  • 2020-12-02 06:54

    You can create any view programmatically (since I am assuming you are asking on how to do this WITHOUT using a LayoutInflater) and call setView on the Toast you made.

        //Create a view here
        LinearLayout v = new LinearLayout(this);
        //populate layout with your image and text or whatever you want to put in here
    
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(v);
        toast.show();
    
    0 讨论(0)
  • 2020-12-02 06:54

    I think this is better that we show text of Toast on the image which we pass to the makeImageToast function... so I shades Knickedi codes and :

    public class utility  {
    
    public static Toast makeImageToast(Context context, int imageResId, CharSequence text, int length) {
        Toast toast = Toast.makeText(context, text, length);
    
        View rootView = toast.getView();
        LinearLayout linearLayout = null;
        View messageTextView = null;
    
        // check (expected) toast layout
        if (rootView instanceof LinearLayout) {
            linearLayout = (LinearLayout) rootView;
    
            if (linearLayout.getChildCount() == 1) {
                View child = linearLayout.getChildAt(0);
    
                if (child instanceof TextView) {
                    messageTextView = (TextView) child;
                    ((TextView) child).setGravity(Gravity.CENTER);
    
                }
            }
        }
    
        // cancel modification because toast layout is not what we expected
        if (linearLayout == null || messageTextView == null) {
            return toast;
        }
    
        ViewGroup.LayoutParams textParams = messageTextView.getLayoutParams();
        ((LinearLayout.LayoutParams) textParams).gravity = Gravity.CENTER;
    
        // convert dip dimension
        float density = context.getResources().getDisplayMetrics().density;
        int imageSize = (int) (density * 25 + 0.5f);
        int imageMargin = (int) (density * 15 + 0.5f);
    
        // setup image view layout parameters
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(imageSize, imageSize);
        imageParams.setMargins(0, 0, imageMargin, 0);
        imageParams.gravity = Gravity.CENTER;
    
        // setup image view
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageResId);
        imageView.setLayoutParams(imageParams);
    
    
        // modify root layout
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackgroundResource(imageResId);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setHorizontalGravity(Gravity.CENTER);
        linearLayout.setHorizontalGravity(Gravity.CENTER);
        //addView(imageView, 0);
    
        return toast;
    }
    

    }

    and this is use of it:

    utility.makeImageToast(getApplicationContext(),
                     R.drawable.your_image,"your_text",Toast.LENGTH_LONG).show();
    
    0 讨论(0)
提交回复
热议问题