当前位置:网站首页>顶部下滑沉浸式dialog

顶部下滑沉浸式dialog

2022-06-13 05:22:00 -SOLO-

研究了一下怎么实现顶部下滑沉浸式dialog。
效果如下。
在这里插入图片描述

在这里插入图片描述

代码如下

   public void showDialog(View view) {
    
        Dialog dialog = new Dialog(this);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null);
        dialog.setContentView(dialogView);
        dialog.getWindow().getDecorView().setBackgroundColor(Color.RED);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.flags = WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    
            params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }
        params.height = 350;

        dialog.getWindow().setGravity(Gravity.TOP);
        dialog.getWindow().setAttributes(params);
        dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        dialog.getWindow().setDimAmount(0);


        dialog.getWindow().getDecorView().setPadding(0, 0, 0, 0);
        dialog.show();

    }

动画
dialog_in

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromYDelta="-100%" android:toYDelta="0%" android:duration="300">

</translate>

dialog_out

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromYDelta="0%" android:toYDelta="-100%" android:duration="300">

</translate>

style

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="dialogWindowAnim" parent="android:Animation"> <item name="android:windowEnterAnimation" >@anim/dialog_in</item> <item name="android:windowExitAnimation" >@anim/dialog_out</item> </style>
</resources>

说明

dialog的宽高控制

主要是通过设置其window的layoutParam来实现的。

      WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = 350;

dialog 横向全屏

必须设置两点。一个人dialog的window的DecorView的padding必须为0。默认不为0。 DecorView必须设置背景。否则可能显示不全

    dialog.getWindow().getDecorView().setBackgroundColor(Color.RED);
       dialog.getWindow().getDecorView().setPadding(0, 0, 0, 0);

dialog沉浸式

要想沉浸式必须设置如下代码

   params.flags = WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    
            params.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
        }

动画

     dialog.getWindow().setWindowAnimations(R.style.dialogWindowAnim);

dialog背景全透明

主要是设置如下

  dialog.getWindow().setDimAmount(0);
原网站

版权声明
本文为[-SOLO-]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_22706515/article/details/124968291