当前位置:   article > 正文

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

android dialog层级

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

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

正常情况下

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

在这里插入图片描述

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

在这里插入图片描述

  • 通过id来设置颜色,和层级
window.setType(WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW);
  • 1
  • 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};
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号