顶部下滑沉浸式dialog

研究了一下怎么实现顶部下滑沉浸式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);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值