Android Toast全面屏适配 全屏显示效果 覆盖状态栏全屏显示 两种实现方式

要做一个全屏显示的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适配全面屏 全屏显示覆盖状态栏 简单实用只需两步

Android中的`Toast`通常用于快速向用户显示简短的信息,但它默认不会全屏显示。如果需要模拟全屏效果,可以采用一些技巧。一种常见的做法是自定义一个全屏Dialog或者Activity,然后将`Toast`的内容添加到这个全屏容器中。 首先,创建一个全屏的布局文件(如fullscreen_layout.xml),包含一个TextView或者其他适合显示文本的组件: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/fullscreen_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:textColor="#fff" android:background="#000" /> </LinearLayout> ``` 然后,在Activity或Fragment中使用`WindowManager`来显示这个全屏的布局: ```java import android.app.Activity; import android.content.Context; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Toast; import android.view.WindowManager; public class CustomToast { private static final int LENGTH_LONG = Toast.LENGTH_LONG; private static final int LENGTH_SHORT = Toast.LENGTH_SHORT; public static void fullScreenToast(Context context, String message, int duration) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.fullscreen_layout, null); TextView textView = layout.findViewById(R.id.fullscreen_text_view); textView.setText(message); WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_TOAST, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT ); params.gravity = Gravity.CENTER垂直方向|Gravity.CENTER水平方向; ((Activity) context).getWindow().addContentView(layout, params); if (duration == LENGTH_SHORT) { // 短时间展示 Handler handler = new Handler(); handler.postDelayed(() -> ((Activity) context).getWindow().removeView(layout), duration); } else { // 长时间展示 Toast.makeText(context, message, LENGTH_LONG).cancel(); } } } ``` 使用时只需调用`CustomToast.fullScreenToast(context, "你的消息", LENGTH_SHORT / LENGTH_LONG)`即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值