Android底部sheet对话框透明背景

Android bottomsheet dialog tansparent background

我制作了一个带有框架布局的底部 sheet 对话框。一切正常,但我无法使背景透明。

我已经为对话框的框架布局和父布局设置了透明颜色UI。

这是框架布局的代码:

<FrameLayout
            android:id="@+id/nearby_bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" />

底部sheet对话框的代码UI:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginStart="6dp"
        android:layout_marginEnd="6dp"
        app:cardCornerRadius="20dp"
        app:cardElevation="10dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

           ....
        </LinearLayout>

    </com.google.android.material.card.MaterialCardView>
</LinearLayout>

这是我初始化对话框的方式:

FrameLayout bottom_sheet;
bottomSheetBehavior = BottomSheetBehavior.from(bottom_sheet);
View view = getLayoutInflater().inflate(R.layout.nearby_floating_sheet, null);
bottomSheetDialog = new BottomSheetDialog(getActivity());
bottomSheetDialog.setContentView(view);
bottomSheetDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
((View)view.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
bottomSheetDialog.show();

这是我的输出:

有人可以帮我解决这个问题吗?

也许您还应该将对话框 Window 设置为透明。

将以下代码添加到您的 BottomSheetDialog。

// BottomSheetDialog
public BottomSheetDialog(Context context) {
    super(context, R.style.Bottom_Sheet_Style); //set your own dialog theme
    // ...
    Window window = getWindow();
    window.setBackgroundDrawableResource(android.R.color.transparent);       
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.alpha = 1.0f;
    lp.dimAmount = 0.0f; 
    window.setAttributes(lp);
   // ...
}

然后将以下代码添加到您的 res/values/styles.xml。如果您没有该文件,请创建一个。

<style name="Bottom_Sheet_Style" parent="@android:style/Theme.Dialog">  
    <item name="android:windowBackground">@android:color/transparent</item>  
    <item name="android:windowNoTitle">true</item>  
    <item name="android:backgroundDimEnabled">true</item>  
</style>

对于透明背景,您必须在创建对话框时应用主题 首先创建以下样式

<item name="android:background">@android:color/transparent</item>

然后使用 BottomSheetDialog(this,R.style.yourStyle)

应用它

您可以使用bottomSheetDialog.getWindow().setDimAmount(0)设置暗淡量