1.代码:
public static void showCustomToast(Activity activity){ Typeface font = ResourcesCompat.getFont(activity, R.font.myfont);//获取res/font路径下的字体 TextView textView=new TextView(activity); textView.setTypeface(font);//字体 textView.setText("自定义Toast"); textView.setTextColor(Color.WHITE); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,20); textView.setPadding(DpUtils.dp2px(20),DpUtils.dp2px(20),DpUtils.dp2px(20),DpUtils.dp2px(20)); GradientDrawable gd=new GradientDrawable(); gd.setCornerRadius(DpUtils.dp2px(20));//圆角 gd.setColor(Color.parseColor("#99000000"));//黑色透明背景 textView.setBackground(gd); Toast toast=new Toast(activity); toast.setView(textView); toast.setGravity(Gravity.CENTER,0,0);//区中 toast.show();//显示 } public static int dp2px(float dipValue) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dipValue,activity.getResources().getDisplayMetrics()); }