Transparent AlertDialog has black background

后端 未结 4 1825
温柔的废话
温柔的废话 2020-11-27 16:31

I have a custom AlertDialog style that makes the AlertDialog box transparent. It works fine except that when I inflate my desired transparent layo

相关标签:
4条回答
  • 2020-11-27 16:42

    In your R.layout.tabs,

    Replace

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabLayout"
        android:background="#000000ff">
    

    with

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:id="@+id/tabLayout"
        android:background="@android:color/transparent">
    
    0 讨论(0)
  • 2020-11-27 16:46

    Those who are having still problem creating custom transparent dialog or alert dialog, can use this combination. I also wanted to show custom background this is what worked for me.

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    

    example:-

    /**
     * alert dialog
     */
    
        public class ToolTipView extends AlertDialog {
    
            public ToolTipView(@NonNull Context context) {
                super(context);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.dialog_tool_tip_view);
            }
    
        }
    
    0 讨论(0)
  • 2020-11-27 16:52

    Have you tried using something like this:

    <style name="CustomDialog" parent="@android:style/Theme.Translucent">
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        ...
    </style>
    

    EDIT

    Try this in java code

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    
    0 讨论(0)
  • 2020-11-27 17:02

    The problem is that AlertDialog builder is actually not good for designing transparent dialog and will and always have this black background which is actually a Theme for it, instead use the Dialog to create a transparent theme instead.

    sample:

    Dialog alertDialog = new Dialog(this);
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    alertDialog.setContentView(R.layout.tabs);
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    alertDialog.show();
    

    Using Dialog does not require any theme manipulation for transparent background so it is basically easy.

    0 讨论(0)
提交回复
热议问题