public class CustomToast { public static void customToastShow(Context context,String content,int duration) { Toast toast=new Toast(context);//实例化一个Toast toast.setDuration(duration);//设置Toast持续时间 toast.setGravity(Gravity.BOTTOM, 0, 100);//设置Toast显示位置 LinearLayout toastLayout=new LinearLayout(context); toastLayout.setOrientation(LinearLayout.HORIZONTAL); toastLayout.setGravity(Gravity.BOTTOM);//此处默认设置为从底部弹出 TextView tv_content=new TextView(context);//实例化一个TextView tv_content.setText(content);//给Textview赋值 tv_content.setTextSize(16);//设置TextView字体大小 tv_content.setTextColor(android.graphics.Color.WHITE);//设置字体颜色 tv_content.setGravity(Gravity.CENTER);//设置TextView在Toast中显示的位置 tv_content.setBackgroundResource(R.drawable.toast_bg);//设置TextView的背景 toastLayout.addView(tv_content, LinearLayout.LayoutParams.WRAP_CONTENT, 100); //设置Toast宽高 toast.setView(toastLayout); toast.show(); } }
代码中所用到的Toast背景图
操作
CustomToast.customToastShow(getApplicationContext()," 再按一次退出 ", Toast.LENGTH_LONG);