简体   繁体   English

如何在Android的自定义警报对话框中透明

[英]How to Transparent in Custom Alert Dialog in android

I created the custom alert dialog for my project. 我为项目创建了自定义警报对话框。 When I click the button, the custom alert dialog is opened but a white background appears on my custom alert dialog. 当我单击按钮时,将打开“自定义警报”对话框,但在我的“自定义警报”对话框上会出现白色背景。 How can I make it transparent? 如何使其透明?

.java: .java:

 package com.example.bskes.customalertdialog;

    import android.content.DialogInterface;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

    public class MainActivity extends AppCompatActivity {
        private Button adbtn;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            adbtn = (Button) findViewById(R.id.btnCustomAD);
            adbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    view = LayoutInflater.from(MainActivity.this).inflate(R.layout.rtr_mem_profile_layout, null);

                    builder.setPositiveButton("Thank You", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "It's My Pleasure", Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.setView(view);
                    builder.show();
                }
            });
        }
    }

You should use this ,this will help to show transparent background of dilog 您应该使用它,这将有助于显示透明的dilog背景

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

First line show the transparent layer on background and second line provide the actual size of the your window view. 第一行显示背景上的透明层,第二行提供窗口视图的实际大小。 it help me, use it 它帮助我,使用它

define what follows in the styles.xml file 定义styles.xml文件中的内容

<style name="CustomDialog" parent="android:Theme.Dialog">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

and pass it as argument to the AlertDialog constructor 并将其作为参数传递给AlertDialog构造函数

AlertDialog.Builder imageDialog = new AlertDialog.Builder(SubProducts.this, R.style.CustomDialog);

Or programmatically, through the Dialog instance you can call 或以编程方式,通过Dialog实例,您可以调用

myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))

Add this to your styles.xml file 将此添加到您的styles.xml文件中

  <style name="MyDialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">false</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

Add this to your java file : 将此添加到您的java文件:

Dialog dialog = new Dialog(this, R.style.MyDialog);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM