Adding image to Toast?

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

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

8条回答
  •  日久生厌
    2020-12-02 06:49

    Yes, you can add imageview or any view into the toast notification by using setView() method, using this method you can customize the Toast as per your requirement.

    Here i have created a Custom layout file to be inflated into the Toast notification, and then i have used this layout in Toast notification by using setView() method.

    cust_toast_layout.xml

    
    
    
        
        
    
        
        
    
        
        
    
    
    

    CustomToastDemoActivity.java

    LayoutInflater inflater = getLayoutInflater();
    View view = inflater.inflate(R.layout.cust_toast_layout, 
        (ViewGroup)findViewById(R.id.relativeLayout1));
    
    Toast toast = new Toast(this);
    toast.setView(view);
    toast.show();
    

提交回复
热议问题