如何解决不推荐使用toast.getView和toast.setView
我想向用户展示自定义吐司。但是toast.getView()和toast.setView()在android studio中已被弃用。
这是我的代码:
Toast toast = Toast.makeText(context,"Show Toast",Toast.LENGTH_LONG);
View view = toast.getView();
view.setBackgroundResource(R.drawable.toast_background);
TextView text = view.findViewById(android.R.id.message);
text.setTextColor(Color.WHITE);
text.setPadding(15,15,0);
toast.show();
现在,如何在android studio(java)中自定义吐司了?
解决方法
新建类
public class WhiteCustomToast {
public static void Make(Context context,String message) {
Toast toast = Toast.makeText(context,message,Toast.LENGTH_SHORT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
View view = toast.getView();
view.setBackgroundResource(R.drawable.white_cutom_bg);
TextView text = (TextView) view.findViewById(R.id.message);
//text.setTextColor(context.getResources().getColor(R.color.white));
text.setTextColor(Color.parseColor("#FFFFFFFF"));
text.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
text.setTypeface(Utilities.Ubuntu_Regular);
text.setTextSize(14f);
}
toast.show();
}
}
在主类或任何地方使用
WhiteCustomToast.Make(context,"Checking");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。