要做一个全屏显示的Toast,用了网上找的方法给Toast添加如下属性
toast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//设置Toast可以布局到系统状态栏的下面
试了下,是有点效果,可以填充到状态栏。但是问题来了,全面屏顶部和底部都会留一个小缝隙不能覆盖。
想了两种解决方案:
第一用AlertDialog实现。
第二用PopupWindow实现。
实现弹出效果后,定时关闭即可。验证都可以实现想要的效果。
1.AlertDialog天生带有全屏显示的能力,你只需要自定义要显示的效果,显示即可。不放代码了,大家自行尝试。
2.PopupWindow需要配置一些属性才可以实现。复制以下代码,直接使用即可
/**
* 全屏展示纯文本toast
*AppManager.getInstance().app//自己项目的application的Context'
* @param text
*/
public static void ToastShortFullScreen(View baseView, CharSequence text) {
LayoutInflater layoutInflater = (LayoutInflater) AppManager.getInstance().app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout_dormitory_submit_check_view, null);
TextView tvMessage = view.findViewById(R.id.tv_custom);
tvMessage.setText(text);
int heiht = AppManager.getInstance().app.getResources().getDisplayMetrics().heightPixels;
final PopupWindow popupWindow = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, height + getStatusBarHeight(mContext) + getBottomKeyboardHeight(AppManager.getInstance().app));
popupWindow.setAnimationStyle(R.style.scale_pop_window_anim_style);//添加动画效果
popupWindow.setFocusable(false);
popupWindow.setBackgroundDrawable(new ColorDrawable(AppManager.getInstance().app.getResources().getColor(R.color.translucence)));
popupWindow.setClippingEnabled(false);
popupWindow.showAtLocation(baseView, Gravity.NO_GRAVITY, 0, -20);
//用RxJava实现定时3秒
Observable.timer(3000, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
popupWindow.dismiss();
}
});
}
如果需要更好地过渡效果,可以给popupWindow加上动画
styles.xml中代码
<!-- 左近右出动画的style -->
<style name="scale_pop_window_anim_style">
<item name="android:windowEnterAnimation">@anim/left_in</item>
<!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/right_out</item>
<!-- 指定消失的动画xml -->
</style>
left_in.xml动画代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:duration="300"
android:fromAlpha="0"
android:toAlpha="1" />
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0" />
</set>
right_out.xml动画代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:duration="300"
android:fromAlpha="1"
android:toAlpha="0" />
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:toXDelta="100%p"/>
</set>
里面获取状态栏和虚拟按键的方法见文章:Android PupopWindow适配全面屏 全屏显示覆盖状态栏 简单实用只需两步