How to set Transparent Background as a Custom Dial

2020-05-20 09:07发布

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

Sample Code :

Dialog dialog;
@Override
protected Dialog onCreateDialog(int id) 
{ 
    switch(id) 
    {
        case 1:
            AlertDialog.Builder builder = null;
            Context mContext = this;
            LayoutInflater inflater = ( LayoutInflater ) mContext.getSystemService( LAYOUT_INFLATER_SERVICE );
            View layout = inflater.inflate( R.layout.about_page, ( ViewGroup ) findViewById( R.id.about_Root ) );
            builder = new AlertDialog.Builder( mContext );
            builder.setView( layout );
            dialog = builder.create();
            break;
    }
    return dialog;
}

How do set Transparent Dialog Box.

My XML Code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/about_Root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/alert_page_border"
    android:orientation="vertical" >        
    <TextView 
        android:id="@+id/about_Title"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:gravity="center"
        android:layout_weight="1"
        android:textSize="25dip"
        android:textColor="@android:color/white"
        android:textStyle="bold|italic"
        android:text="Welcome" />
</LinearLayout>

Code Alert_page_border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">     
    <solid android:color="#3b3b3b" />
    <stroke 
        android:width="3dp"
        android:color="#ffffff" />
    <padding 
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" /> 
    <corners 
        android:radius="2dp" />     
</shape>

Sample Screenshot :

enter image description here

How do avoid this block color.

8条回答
时光不老,我们不散
2楼-- · 2020-05-20 09:31
 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/a"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:background="#00000000">

 <!-- Write your LinearLayout code here -->

 </RelativeLayout>

Try this code.

查看更多
叛逆
3楼-- · 2020-05-20 09:43

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.

查看更多
登录 后发表回答