Android 對話方塊【Dialog】去除白色邊框代碼

來源:互聯網
上載者:User

使用樣式檔案,在values 目錄下建立styles.xml檔案,編寫如下代碼:

 

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

 

 

調用時,使用AlerDialog的介面類,Dialog 介面編寫如下代碼:

 

    Dialog dialog = new Dialog(SetActivity.this, R.style.dialog);
                    dialog.setContentView(R.layout.test);
                    dialog.show();

 

下面我們查看一下Dialog的源碼檔案,裡面的建構函式為如下:

 

public Dialog(Context context, int theme) {
        mContext = new ContextThemeWrapper(
            context, theme == 0 ? com.android.internal.R.style.Theme_Dialog : theme);
        mWindowManager = (WindowManager)context.getSystemService("window");
        Window w = PolicyManager.makeNewWindow(mContext);
        mWindow = w;
        w.setCallback(this);
        w.setWindowManager(mWindowManager, null, null);
        w.setGravity(Gravity.CENTER);
        mUiThread = Thread.currentThread();
        mDismissCancelHandler = new DismissCancelHandler(this);
    }

 

這裡面我們可以看出,Android 使用了預設的建構函式為Dialog 設定樣式,如果沒有為其設定樣式,即預設載入事先編寫好的樣式檔案,Dialog 一共由多個9.png的圖片構成,大部分都是帶有邊框的9.png圖片,所以就是為什麼我們上邊的樣式檔案要將其背景去除掉。這個東西搞了我好久,希望對你有協助

 

 

前後效果對比

未設定前:

 

設定後:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.