DialogFragment 透明全屏设置终极实用办法

来源:互联网 发布:网络连接不可用 编辑:程序博客网 时间:2024/06/14 01:56

今天在项目中需要用到DialogFragment,并且需要全屏透明;

苦逼的是怎么弄两边都会有一点间距;

这是因为DialogFragment 弹出框默认是在屏幕的中央,左右还有留白,那么怎么样才能将这左右的留白去掉呢?

这里写图片描述

红色是我想去掉的地方

答案很简单(但是我还没弄明白):

@Overridepublic void onStart(){super.onStart();DisplayMetrics dm = new DisplayMetrics();getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );getDialog().getWindow().setLayout( dm.widthPixels, getDialog().getWindow().getAttributes().height );}

在你的DialogFragment加上上面这段话就可以了;

我还没搞懂原理,如果你明白原理请在评论里留言,谢谢!

另外设置透明就比较简单了,这是我的style:

<style name="TranslucentNoTitle" parent="android:style/Theme.Dialog">        <item name="android:windowNoTitle">true</item>        <item name="android:background">@android:color/transparent</item>        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowFullscreen">true</item>        <item name="android:colorBackgroundCacheHint">@null</item>    </style>
2 0