android - 透明背景不适用于 Bottom Sheet

标签 android bottom-sheet

我的代码如下所示:

public class OTP extends BottomSheetDialogFragment {

    private RecyclerView recyclerView;
    private OTPAdapter otpAdapter;
    private List<demand.inn.com.quflip.response.OTP> otpList = new ArrayList<>();

    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() {

        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState) {
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                dismiss();
            }

        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset) {
        }
    };

    public static OTP newInstance(ArrayList<demand.inn.com.quflip.response.OTP> otpList) {
        OTP otp = new OTP();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(Consts.OTP_LIST, otpList);
        otp.setArguments(bundle);
        return otp;
    }


    @Override
    public void setupDialog(Dialog dialog, int style) {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.fragment_otp, null);
        otpList = getArguments().getParcelableArrayList(Consts.OTP_LIST);
        otpAdapter = new OTPAdapter(getActivity(), otpList);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.recycler_view);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(otpAdapter);
        dialog.setContentView(contentView);
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
        CoordinatorLayout.Behavior behavior = params.getBehavior();

        if (behavior != null && behavior instanceof BottomSheetBehavior) {
            ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);
        }

    }

}

fragment_otp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/reviewLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/transparent"
        android:orientation="vertical"
        android:visibility="visible">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@android:color/transparent"
            android:clipToPadding="false"
            android:paddingTop="10dp" />
    </RelativeLayout>

</LinearLayout>

card_otp.xml
<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/transparent"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <TextView
            android:id="@+id/truckName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Taco Bell"
            android:textColor="@color/colorGray" />

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|right"
                android:text="OTP"
                android:textColor="@color/colorGray"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/otp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|right"
                android:text="2256"
                android:textColor="@color/colorRoyalPurple"
                android:textSize="18sp" />

        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="@color/colorMercuryE1" />

</LinearLayout>

在上面的代码中,我已经放置了透明背景,但是仍然无法正常工作。我尝试以编程方式执行此操作,但是没有任何效果。有什么方法/标志可以通过它实现?

最佳答案

这么晚的答案,但尝试这个。您可以尝试将新样式添加为透明的:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="BottomSheet" parent="@style/Widget.Design.BottomSheet.Modal">

        <item name="android:typeface">monospace</item>
        <item name="android:background">@drawable/shape_rounded_bottom_sheet_dialog</item>
    </style>

    <style name="BaseBottomSheetDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheet</item>
    </style>

    <style name="BottomSheetDialogTheme" parent="BaseBottomSheetDialog" />


    <style name="BottomSheetTransparent" parent="@style/Widget.Design.BottomSheet.Modal">

        <item name="android:typeface">monospace</item>
        <item name="android:background">@color/fully_transparent</item>
    </style>

    <style name="BaseBottomSheetTransparentDialog" parent="@style/Theme.Design.Light.BottomSheetDialog">
        <item name="bottomSheetStyle">@style/BottomSheetTransparent</item>
    </style>

    <style name="BottomSheetTransparentDialogTheme" parent="BaseBottomSheetTransparentDialog" />
</resources>

关于android - 透明背景不适用于 Bottom Sheet ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44562901/

相关文章:

android - ActionBar 中的 SearchView - 搜索未答复

android - 用于嵌套 json 的 POJO

android - 为什么我的bottomSheet 来自jetpack compose 中的屏幕顶部

状态更改后的 Android Bottom Sheet

android - 如何制作依赖于另一个应用程序的Android应用程序?

android - 使用 Adob​​e Reader 在 Android 中打开 PDF

android - 谷歌资料库更新后, Bottom Sheet View 无法完全展开吗?

java - 在 Bottom Sheet 中更新 recyclerview 内容失败

android - Bottom Sheet 内的谷歌地图 View

android - 如何在 flutter 中为底部导航栏图标提供渐变