remove border, padding from Dialog

白昼怎懂夜的黑 提交于 2019-11-27 10:33:21

问题


I have an activity which is with the theme Theme.Transparent which is:

<style name="Theme.Transparent" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
    <item name="android:gravity">top</item>
</style>

i'm trying to get rid of the border and the padding around it.. i want to make fill the horizontal of the screen. and no gray border. please help :)


回答1:


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>



回答2:


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

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



回答3:


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;



回答4:


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!



来源:https://stackoverflow.com/questions/8836938/remove-border-padding-from-dialog

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