繁体   English   中英

自定义AlertDialog的透明背景

[英]Transparent background for custom AlertDialog

奇怪的是,这个工作正常,这就是我在Google Play上发布的应用程序的外观:

在此输入图像描述

这就是它现在的样子:

在此输入图像描述

我所做的就是迁移到AndroidX。

这是我的对话框布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialogLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="@drawable/rounded_corners">

        <Button
            android:id="@+id/btnAdd2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/import_video"
            android:textColor="@color/dark_text" />

    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corners">

        <Button
            android:id="@+id/btnAdd1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:text="@string/add_student"
            android:textColor="@color/dark_text" />

    </FrameLayout>

</LinearLayout>

这是我如何膨胀它:

View dialogView = View.inflate(getApplicationContext(), R.layout.dialog_main, null);
LinearLayout dialogLinear = dialogView.findViewById(R.id.dialogLinear);
//Here is where is set the background to transparent
dialogLinear.setBackgroundColor(0x00000000);

final AlertDialog alertD = new AlertDialog.Builder(this).create();
Button btnAdd1 = dialogView.findViewById(R.id.btnAdd1);
Button btnAdd2 = dialogView.findViewById(R.id.btnAdd2);
btnAdd1.setTypeface(mCustom_font_Bold);
btnAdd2.setTypeface(mCustom_font_Bold);           

btnAdd1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Do stuff
    }
});

btnAdd2.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //Do stuff
    }
});

alertD.setView(dialogView);
if (alertD.getWindow() != null) {
    alertD.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
}
alertD.show();

我搜索过,找不到为什么会发生这种情况。 有人可以给我一些建议吗?


我已尝试在布局中设置它。


编辑1:

在尝试下面的答案后,它现在看起来像这样,而不是上面:

在此输入图像描述


通过添加以下样式解决了它

<style name="NewDialog">
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:background">@android:color/transparent</item>
</style>

设置这样的样式:

final AlertDialog alertD = new AlertDialog.Builder(this, R.style.NewDialog).create();

为了解决编辑1中发生的事情,我将LinearLayout更改为RelativeLayout

  1. 您可以创建主题并将该主题分配给AlertDialog

styles.xml定义主题

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

并将该主题分配给AlertDialog

final AlertDialog alertD = new AlertDialog.Builder(this,R.style.CustomDialog).create();

要么

2.您应该尝试使用Dialog而不是本答案中的AlertDialog As Explained。

暂无
暂无

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

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