自定义AlertDialog的透明背景

问题描述 投票:0回答:1

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

enter image description here

这就是它现在的样子:

enter image description here

我所做的就是迁移到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:

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

enter image description here


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

<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

java android android-alertdialog
1个回答
0
投票
  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 in this Answer

© www.soinside.com 2019 - 2024. All rights reserved.