DialogFragment用法示例

 @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        if (window != null) {
            //设置 window 的背景色为透明色.
            //如果通过 window 设置宽高时,想要设置宽为屏宽,就必须调用下面这行代码。
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            WindowManager.LayoutParams attributes = window.getAttributes();
            //在这里我们可以设置 DialogFragment 弹窗的位置
            attributes.gravity = Gravity.BOTTOM;
            //我们可以在这里指定 window的宽高
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            // 设置窗体的高度
//        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            attributes.height = Utils.getScreenHeight(mContext)*3/4;
            window.getDecorView().setPadding(0, 0, 0, 0);
            //设置 DialogFragment 的进出动画
//            attributes.windowAnimations = R.style.DialogAnimation;
            window.setAttributes(attributes);
        }
    }
————————————————
版权声明:本文为CSDN博主「qq3290510686」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36388797/article/details/107211032

@NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog;
        //设置位置
        dialog = super.onCreateDialog(savedInstanceState);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        return dialog;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSourceType = MediaConstant.NONE_MUSIC;
        Bundle args = getArguments();
        if (args != null) {
            mSourceType = args.getInt(MediaConstant.EXTRAS_SOURCE_TYPE);
        }
        MLog.d(TAG, "mSourceType: " + mSourceType);
    }

    @Nullable
    @Override
    public View onCreateView(
            LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        //设置背景
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(getActivity()
                .getDrawable(R.drawable.bg_main_color));
        return inflater.inflate(R.layout.fragment_no_header_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        bindViewModel();
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置宽高
        int width = ScreenUtil.dp2px(getActivity(), 1440f);
        int height = ScreenUtil.dp2px(getActivity(), 824f);
        getDialog().getWindow().setLayout(width, height);
    }

开启Fragment事务,将DialogFragment加到回退栈中
show()方法进行显示

    private fun showDialog(str: String, item: Equipment) {
        val ft: FragmentTransaction = parentFragmentManager.beginTransaction()
        val prev: Fragment? = getFragmentManager()?.findFragmentByTag("dialog")
        if (prev != null) {
            ft.remove(prev)
        }
        ft.addToBackStack(null)
        val newFragment: mDialog= mDialog.newInstance(item)
        newFragment.show(ft, "dialog")
        }
    }

 DialogFragment使用及注意事项 - 简书

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值