Android 4.4 Dialog 被状态栏遮挡的解决方法

来源:互联网 发布:gcp网络报名 编辑:程序博客网 时间:2024/05/31 19:00

首先看不正常的图,点击tracing_dialog按钮弹出对话框
这里写图片描述

然后看理论上的效果图
这里写图片描述

观察两张图发现,不正常的图最上方被状态栏遮挡住了,而该问题存在于android4.4版本中。为了修复该问题,我们增加一个函数在Dialog的子类中,对于android4.4及以上版本进行修复,而android4.4以下版本不进行处理。

我们先来看有问题的代码

package cn.edu.zafu.demo;import android.app.Dialog;import android.content.Context;import android.os.Build;import android.os.Bundle;import android.view.WindowManager;/** * Created by lizhangqu on 2015/5/22. */public class TracingDialog extends Dialog {    public TracingDialog(Context mContext, int style) {        super(mContext, style);        setCancelable(false);    }    protected void onCreate(Bundle paramBundle) {        setContentView(R.layout.tracing_dialog);    }}

创建Dialog的方法如下,第一个参数是Context对象,第二个参数是主题文件对应的id

TracingDialog dialog=new TracingDialog(MainActivity.this, R.style.kdialog);dialog.show();

style如下

<style name="kdialog" parent="@android:style/Theme.Dialog">    <item name="android:windowBackground">@android:color/transparent</item>    <item name="android:windowFrame">@null</item>    <item name="android:windowNoTitle">true</item>    <item name="android:windowIsFloating">true</item>    <item name="android:windowIsTranslucent">false</item>    <item name="android:backgroundDimEnabled">false</item></style>

现在我们在TracingDialog中增加一个函数,该函数对android4.4及以上版本进行适配使其显示正常,增加的函数如下

private void applyCompat() {    if (Build.VERSION.SDK_INT < 19) {        return;    }    getWindow().setFlags(            WindowManager.LayoutParams.FLAG_FULLSCREEN,            WindowManager.LayoutParams.FLAG_FULLSCREEN);}

在TracingDialog的onCreate方法中调用以上函数即可,如下

protected void onCreate(Bundle paramBundle) {    applyCompat();    setContentView(R.layout.tracing_dialog);}

姑且不考虑继承Dialog这种创建Dialog的方法,没办法,历史遗留问题。Dialog的创建方法官方已经建议使用DialogFragment进行创建了。就这样,一个函数解决了问题!

参考文章

  • dialog-on-android-kitkat-seems-to-be-cut
0 0
原创粉丝点击