Dialog with transparent background in Android

前端 未结 20 2432
孤城傲影
孤城傲影 2020-11-22 15:02

How do I remove the black background from a dialog box in Android. The pic shows the problem.

\"enter

相关标签:
20条回答
  • 2020-11-22 15:26

    Make sure R.layout.themechanger has no background color because by default the dialog has a default background color.

    You also need to add dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

    And finally

    <style name="TransparentDialog">
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 15:30

    You can use the:

    setBackgroundDrawable(null);
    

    method.And following is the doc:

      /**
        * Set the background to a given Drawable, or remove the background. If the
        * background has padding, this View's padding is set to the background's
        * padding. However, when a background is removed, this View's padding isn't
        * touched. If setting the padding is desired, please use
        * {@link #setPadding(int, int, int, int)}.
        *
        * @param d The Drawable to use as the background, or null to remove the
        *        background
        */
    
    0 讨论(0)
  • 2020-11-22 15:31

    One issue I found with all the existing answers is that the margins aren't preserved. This is because they all override the android:windowBackground attribute, which is responsible for margins, with a solid color. However, I did some digging in the Android SDK and found the default window background drawable, and modified it a bit to allow transparent dialogs.

    First, copy /platforms/android-22/data/res/drawable/dialog_background_material.xml to your project. Or, just copy these lines into a new file:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="16dp">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="?attr/colorBackground" />
        </shape>
    </inset>
    

    Notice that android:color is set to ?attr/colorBackground. This is the default solid grey/white you see. To allow the color defined in android:background in your custom style to be transparent and show the transparency, all we have to do is change ?attr/colorBackground to @android:color/transparent. Now it will look like this:

    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:inset="16dp">
        <shape android:shape="rectangle">
            <corners android:radius="2dp" />
            <solid android:color="@android:color/transparent" />
        </shape>
    </inset>
    

    After that, go to your theme and add this:

    <style name="MyTransparentDialog" parent="@android:style/Theme.Material.Dialog">
        <item name="android:windowBackground">@drawable/newly_created_background_name</item>
        <item name="android:background">@color/some_transparent_color</item>
    </style>
    

    Make sure to replace newly_created_background_name with the actual name of the drawable file you just created, and replace some_transparent_color with the desired transparent background.

    After that all we need to do is set the theme. Use this when creating the AlertDialog.Builder:

        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.MyTransparentDialog);
    

    Then just build, create, and show the dialog as usual!

    0 讨论(0)
  • 2020-11-22 15:32

    I've faced the simpler problem and the solution i came up with was applying a transparent bachground THEME. Write these lines in your styles

        <item name="android:windowBackground">@drawable/blue_searchbuttonpopupbackground</item>
    </style>
    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
    

    And then add

    android:theme="@style/Theme.Transparent"
    

    in your main manifest file , inside the block of the dialog activity.

    Plus in your dialog activity XML set

     android:background= "#00000000"
    
    0 讨论(0)
  • 2020-11-22 15:34

    Attention : Don't use builder for changing background.

    Dialog dialog = new Dialog.Builder(MainActivity.this)
                                    .setView(view)
                                    .create();
    dialog.show();dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    

    change to

    Dialog dialog = new Dialog(getActivity());
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(view);
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.show();
    

    When using Dialog.builder, it's not giving getWindow() option in it.

    0 讨论(0)
  • 2020-11-22 15:35

    use this code it's works with me :

        Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
        dialog.show();
    
    0 讨论(0)
提交回复
热议问题