如何在android中将透明背景设置为自定义对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11864162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:03:16  来源:igfitidea点击:

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

androiddialog

提问by Bala

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 代码:

<?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

代码 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

enter image description here

How do avoid this block color.

如何避免这种块颜色。

回答by ilomambo

Use dialog instead of AlertDialog

使用对话框而不是 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();

回答by Plumillon Forge

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

如果您使用 API >= 11,您可以尝试为您的 Dialog 设置一个主题,如下所示:

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));

回答by Midhu

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.

希望这会帮助你实现你的目标。

回答by Boris Strandjev

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

colors.xml

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

styles.xml:

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.

我不知道为什么我必须创建一个自定义类才能考虑主题。现在我已经实现了我想要做的:透明对话框。接下来的战斗将是定位正负按钮,只要我不能再使用方便的方法AlertDialog.Builder。希望我的代码能证明对你有帮助。

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

还要注意 alpha 的低值 - 甚至可以尝试使用 00 以确保您的配置是否有效。

回答by Varun Bhatia

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

使用dialog.getWindow().setBackgroundDrawable(null)删除默认背景。

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
   */
/*** 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
   */

回答by Deepesh

just remove

只需删除

from Alert_page_border.xml

来自 Alert_page_border.xml

回答by Law Gimenez

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 ) );

回答by Krishna Suthar

 <?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.

试试这个代码。