引言:一搬的Dialog系统默认是不允许全屏的,下面我们介绍几种Dialog全屏的方式

第一、

final Dialog dialog = new Dialog(WenDetailActivity.this, R.style.popupDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.wen_cover_pager);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
dialog.show();
style的样式:
<span style="font-size:14px;"><style name="popupDialog" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
        <item name="android:windowBackground">@drawable/filled_activity_bg</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <!--<item name="android:windowAnimationStyle">@style/dialog_animation</item>-->
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:backgroundDimAmount">0.6</item><!-- 灰度 -->
        <item name="android:windowFullscreen">true</item>
    </style></span>

第二、

我们也可以自定义Dialog,首先继承Dialig,然后再构造函数中添加

super(context, android.R.style.Theme); setOwnerActivity((Activity)context);

第三、

又或者我们设置://dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

也可以让我们的Dialog全屏显示。

注意:按照上述方式设置Dialog全屏,有些朋友可能会发现,Dialog全屏好像并不是我们想要的,因为它把状态栏也遮住了,有时候我们只是想让它遮住标题栏,就是和我们的activity一样大小,就这个问题下面小编介绍一种设置Dialog和activity一样大小的方法。

第三、

首先介绍一个方法:getDecorView()

decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。 于是,我们就可以算出状态栏的高度了。

Rect frame = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
同样我们获取标题栏的高度

        getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,然后就可以知道标题栏的高度了,代码如下:

        int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();         int titleBarHeight = contentTop - statusBarHeight; //statusBarHeight是上面所求的状态栏的高度

最后:知道上述原理,我们就可以设置我们的Dialog和activity一样大了,Java代码如下:

final Dialog dialog = new Dialog(WenDetailActivity.this, R.style.popupDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.wen_cover_pager);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        WindowManager.LayoutParams lay = dialog.getWindow().getAttributes();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        Rect rect = new Rect();
        View view = getWindow().getDecorView();//decorView是window中的最顶层view,可以从window中获取到decorView
        view.getWindowVisibleDisplayFrame(rect);
        lay.height = dm.heightPixels - rect.top;
        lay.width = dm.widthPixels;
style.xml如下:
<style name="popupDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@drawable/filled_activity_bg</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <!--<item name="android:windowAnimationStyle">@style/dialog_animation</item>-->
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:backgroundDimAmount">0.6</item><!-- 灰度 -->
        <!--<item name="android:windowFullscreen">true</item>-->
    </style>
你可能感兴趣的内容
Android中远程Service浅析 收藏,4242 浏览
0条评论

dexcoder

这家伙太懒了 <( ̄ ﹌  ̄)>
Owner