How to set Transparent Background as a Custom Dialog Box in android

前端 未结 8 1064
梦谈多话
梦谈多话 2021-02-02 17:52

I need to make my custom dialog box as a transparent.

Sample Code :

Dialog dialog;
@Override
protected Dialog onCreateDialog(int id) 
{ 
    switch(id) 
         


        
相关标签:
8条回答
  • 2021-02-02 18:09

    This question of yours describes exactly what my problem was. I tried every single solution I stumbled upon in this thread as well as in several more threads. Nothing worked. Finally I stumbled upon this thread and the corresponding answer.

    I had to create a custom dialog class. So I did and it worked. Actually the following styles were sufficient for me:

    colors.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="transparent_black">#66000000</color>
    </resources>
    

    styles.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="custom_dialog_theme" parent="@android:style/Theme.Translucent">
            <item name="android:windowBackground">@color/transparent_black</item>
            <item name="android:windowIsFloating">true</item>
        </style>
    </resources>
    

    The code of the dialog class:

    import android.app.Dialog;
    import android.content.Context;
    
    import my.app.com.android.R;
    
    public class CustomDialog extends Dialog {
        public AddBookCustomDialog(final Context context) {
            super(context, R.style.custom_dialog_theme);
            this.setContentView(R.layout.add_book_dialog);
        }
    }
    

    And how I use this dialog in the activity code:

    CustomDialog customDialog = new CustomDialog(this);
    customDialog.show();
    

    I don't know why I have to create a custom class in order for the theme to be taken into account. Now I have achieved what I wanted to do: transparent dialog. The next fight will be to position positive and negative buttons as long as I can no longer user the convenience methods of AlertDialog.Builder. Hopefully my code will prove of help to you.

    Also notice the low value of alpha - maybe even try it with 00 so that you make sure whether your configuration works.

    0 讨论(0)
  • 2021-02-02 18:12

    Just create a style and apply that to your dialog

        <style name="myDialogTheme" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">@color/Transparent</item>
            <item name="android:windowIsFloating">false</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    

    Then, create your Dialog by using this theme;

        Dialog myDialog = new Dialog(this,R.style.myDialogTheme) {
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.about_page);
            }
        };
    

    Hope this will help you to achieve your goal.

    0 讨论(0)
  • 2021-02-02 18:18

    If you are on API >= 11, you can try to set a theme to your Dialog like that :

    new AlertDialog.Builder(mContext , android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
    

    Or you can try to set the background transparent :

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    
    0 讨论(0)
  • 2021-02-02 18:23

    Try this,

    layout.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    add the code above after this line.

    View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );
    
    0 讨论(0)
  • 2021-02-02 18:24

    Use dialog instead of AlertDialog

    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    dialog.setContentView(R.layout.splash);
    dialog.show();
    
    0 讨论(0)
  • 2021-02-02 18:24

    Use dialog.getWindow().setBackgroundDrawable(null) to remove the default background.

    Here is the doc for reference:

    /*** 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)
提交回复
热议问题