Android中自定义DialogFragment解决宽度和高度问题

Android中自定义DialogFragment解决宽度和高度问题

Android中自定义DialogFragment解决宽度和高度问题但是我们很多时候想把DialogFragment的高度固定,那么我们需要设置DialogFragment的高度,在Fragment的onResume()声明周期方法中设置window的宽高即可。

    @Override
    public void onResume() {
        super.onResume();
            getDialog().getWindow().setLayout(DeviceUtil.getDeviceWidth(), ResUtils.dp2px(295));
    }

设置DialogFrament 从底部弹出,并且弹出动画为向上滑出,消失动画为向下滑出

WindowManager.LayoutParams params = getDialog().getWindow()
        .getAttributes();
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
params.windowAnimations = R.style.bottomSheet_animation;
getDialog().getWindow().setAttributes(params);

完整的代码如下:

 @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        WindowManager.LayoutParams params = getDialog().getWindow()
                .getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        params.windowAnimations = R.style.bottomSheet_animation;
        getDialog().getWindow().setAttributes(params);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().setCanceledOnTouchOutside(true);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mContentView = inflater.inflate(R.layout.fragment_create_quick, container, false);
        return mContentView;
    }

    @Override
    public void onResume() {
        super.onResume();
       getDialog().getWindow().setLayout(DeviceUtil.getDeviceWidth(), HlyUtils.dp2px(380));
    }

 <style name="bottomSheet_animation" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/slide_in_bottom</item>
        <item name="android:windowExitAnimation">@anim/slide_out_bottom</item>
 </style>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="300"
        android:fromYDelta="100.0%p"
        android:toYDelta="0.0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:duration="300"
        android:fromYDelta="0%p"
        android:toYDelta="100%p" />
</set>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容