怎么在Android中使用Toast实现全屏显示?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
private void toastFullScreen(){ Toast toast = Toast.makeText(this, null, Toast.LENGTH_LONG * 10 * 1000); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout)toast.getView(); // Get the screen size with unit pixels. WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); TextView tv = new TextView(this); LayoutParams vlp = new LayoutParams(outMetrics.widthPixels, outMetrics.heightPixels); vlp.setMargins(0, 0, 0, 0); tv.setLayoutParams(vlp); tv.setText("Hello Toast! I am full screen now."); tv.setGravity(Gravity.CENTER); toastView.addView(tv); toast.show(); }
根据实际情况,在toastView添加不同view能显示不同弹窗,希望对大家有帮助!
另一段自定义toast全屏显示实现代码:
public class MyToast { private static Toast mGoodToast; private static ObjectAnimator mObjectAnimator; public static void showGoodToast(Context context) { if (mGoodToast == null) { mGoodToast = new Toast(context); mGoodToast.setGravity(Gravity.CENTER, 0, 0); mGoodToast.setDuration(Toast.LENGTH_LONG); View view = LayoutInflater.from(context).inflate(R.layout.dialog_good, null, false); AppCompatImageView imageView = view.findViewById(R.id.shine); mObjectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 5000); mObjectAnimator.setInterpolator(new LinearInterpolator()); mObjectAnimator.setDuration(30000); mGoodToast.setGravity(Gravity.FILL, 0, 0); mGoodToast.setView(view); mGoodToast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//设置Toast可以布局到系统状态栏的下面 } if (!mObjectAnimator.isRunning()) { mObjectAnimator.start(); } mGoodToast.show(); } }
看完上述内容,你们掌握怎么在Android中使用Toast实现全屏显示的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。