自定义toast(给toast指定布局和显示位置)

    private void showDiyToast(Context context) {
        Toast toast = new Toast(context);//创建一个Toast示例
        toast.setDuration(Toast.LENGTH_SHORT);//设置toast显示的时长
        View layout = View.inflate(context, R.layout.my_toast_layout, null);//加载一个自定义的toast布局文件
        TextView tvContent = layout.findViewById(R.id.tv_content);//获取自定义布局的控件
        tvContent.setText("我是内容");
        toast.setView(layout);//给toast设置布局
        toast.setGravity(Gravity.CENTER, 0, 0);//设置toast在屏幕中的显示位置
        toast.show();
    }

 

你可能感兴趣的:(android,对话框dialog,toast)