remove border, padding from Dialog

纵饮孤独 提交于 2019-11-28 17:33:02

Be sure to create your Dialog referencing your custom theme:

Dialog dialog = new Dialog(this, R.style.MyDialogTheme);

Your custom theme needs to fill the screen and disable a couple of Android framework defaults:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyDialogTheme" parent="android:Theme.Dialog">
        <!-- Fill the screen -->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>

        <!-- No backgrounds, titles or window float -->
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>      

        <!-- Just to prove it's working -->
        <item name="android:background">#ff0000</item>
    </style>

</resources>

Same as above but doing it in code rather than in xml worked for me.

    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Set width and height to match parent container.

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    WindowManager.LayoutParams wmlp = dialog.getWindow()
            .getAttributes();
    wmlp.width = android.view.WindowManager.LayoutParams.MATCH_PARENT;
    wmlp.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT;

The following works perfectly for me. It lets me have a full-width dialog (fills the screen's width with no padding) but with wrap_content for height, and it retains all my other stylings that I do in my builder:

<item name="windowMinWidthMajor">100%</item>
<item name="windowMinWidthMinor">100%</item>

<item name="android:windowBackground">@null</item>
<item name="android:windowIsFloating">true</item>

<item name="android:background">#ffffff</item>

Background is required or else it does a weird repeat thing, but just set this to the color you want your dialog background to be. WindowBackground and WindowIsFloating are required to make the size wrap correctly.

Add your theme to your builder like so:

builder = new AlertDialog.Builder(_context, R.style.DialogTheme); and you're good to go!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!