Adding image to Toast?

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

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

8条回答
  •  旧时难觅i
    2020-12-02 06:44

        class CustomToast extends AppCompatActivity {
        Button custom_toast;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_custom_toast);
    
            custom_toast = (Button) findViewById(R.id.customToast);
            custom_toast.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater=getLayoutInflater();
                    View layout=inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
    
                    TextView toastTextView=(TextView) findViewById(R.id.toastTextView);
                    ImageView toastimageview=(ImageView) findViewById(R.id.toastImageView);
                    toastTextView.setText("Custom toast in android");
                    toastimageview.setImageResource(R.drawable.ic_launcher_background);
    
                    Toast toast=new Toast(CustomToast.this);
                    toast.setDuration(Toast.LENGTH_SHORT);
                    toast.setView(layout);
                    toast.show();
                }
            });
        }
    }
    

提交回复
热议问题