欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

在Android应用中,如何巧妙地管理Dialog层叠结构(使用WindowManager.LayoutParams)

最编程 2024-07-26 16:14:51
...
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}; } }

推荐阅读