Android Dialog之间的层级设置(WindowManager.LayoutParams)

Dialog层级(PopupWindow是在Dialog下面的)

  • 有时需要有多个Dialog,并且要求之间有层级关系。当然可以通过逻辑来控制Dialog弹出的先后顺序。
  • 如果要求后弹出的Dialog在上一个Dialog的底层,可以需要通过层级来控制。

正常情况下

  • 红色弹出第二个弹出,所以在上面

在这里插入图片描述

  • 通过层级修改,让红色弹出在蓝色下面

在这里插入图片描述

  • 通过id来设置颜色,和层级
window.setType(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);
  • WindowManager.LayoutParams.XX有许多种取值,默认是TYPE_APPLICATION

public class DialogFragment1 extends DialogFragment {

    private TextView tvTest;

    private int id;

    public DialogFragment1() {
        // Required empty public constructor
    }


    public void show(@NonNull FragmentManager manager, @Nullable String tag, int id) {
        super.show(manager, tag);
        this.id = id;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        if (getContext() != null) {
            Dialog dialog = new Dialog(getContext(), getTheme());
            Window window = dialog.getWindow();
            if (window != null) {
                window.setDimAmount(0.5f);
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                if (id == 2) {
                    window.setType(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);
                }
            }
            return dialog;
        }
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog, container, false);
        initView(view);
        initAnim();
        return view;
    }

    private void initAnim() {
        //加入动画
        if (getContext() != null && getDialog() != null && getDialog().getWindow() != null) {
            ObjectAnimator.ofFloat(getDialog().getWindow().getDecorView(), "translationX", getScreenSize(getContext())[0], 0).setDuration(1000).start();
        }
    }

    private void initView(View view) {
        tvTest = view.findViewById(R.id.tv_test);
        tvTest.setText(String.valueOf(id));
        if (id == 2) {
            tvTest.setBackgroundColor(Color.RED);
        }
    }

    /**
     * 获取屏幕宽高
     *
     * @param context
     * @return
     */
    private static int[] getScreenSize(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
    }
}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值