Dialog with transparent background in Android

前端 未结 20 2399
孤城傲影
孤城傲影 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: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:

    
        
            
            
        
    
    

    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:

    
        
            
            
        
    
    

    After that, go to your theme and add this:

    
    

    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!

提交回复
热议问题