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:17

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();
查看更多
3楼-- · 2020-05-20 09:18

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 ) );
查看更多
干净又极端
4楼-- · 2020-05-20 09:20

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));
查看更多
地球回转人心会变
5楼-- · 2020-05-20 09:22

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.

查看更多
神经病院院长
6楼-- · 2020-05-20 09:23

just remove

from Alert_page_border.xml

查看更多
混吃等死
7楼-- · 2020-05-20 09:31

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
   */
查看更多
登录 后发表回答