Android 4.4 Dialog 被狀態列遮擋的解決方案

來源:互聯網
上載者:User

Android 4.4 Dialog 被狀態列遮擋的解決方案

首先看不正常的圖,點擊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如下

現在我們在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進行建立了。就這樣,一個函數解決了問題!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.