gpt4 book ai didi

android - BottomSheetDialogFragment View 仅在设备旋转后才变为透明

转载 作者:行者123 更新时间:2023-11-29 18:39:39 34 4
gpt4 key购买 nike

我想创建带圆角的 BottomSheetDialogFragment

我为实现这一目标所做的工作:

    <style name="AppTheme.TransparentBottomSheetStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:background">@android:color/transparent</item>
</style>

<style name="AppTheme.BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.Dialog">
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:colorBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="bottomSheetStyle">@style/AppTheme.TransparentBottomSheetStyle</item>
</style>

backdrop_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="#00FF00"/>

<corners
android:topLeftRadius="24dp"
android:topRightRadius="24dp"/>
</shape>

和底页布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="match_parent"
android:background="@drawable/backdrop_shape"
android:paddingBottom="18dp">

和代码:

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.AppTheme_BottomSheetDialogTheme)
dialog?.window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
}

这适用于 AppCompat 主题,但不适用于 MaterialComponents:

enter image description here

如您所见,有白色背景(我想,它是 ID 为 design_bottom_sheet 的父 FrameLayout)。

最有趣的是,当我旋转设备(然后将其旋转回来)时,它会变成我想要的透明:

enter image description here

它是错误还是功能?如何解决?

注意:我已经尝试了所有发布在 SO 和其他网站上的解决方案,它们也不适用于 MaterialComponents

最佳答案

根据 DialogFragment.setStyle :

Call to customize the basic appearance and behavior of the fragment's dialog. This can be used for some common dialog behaviors, taking care of selecting flags, theme, and other options for you. The same effect can be achieve by manually setting Dialog and Window attributes yourself. Calling this after the fragment's Dialog is created will have no effect.

我猜也许 setStyle 应该在 onCreate 中或之前被调用。

关于android - BottomSheetDialogFragment View 仅在设备旋转后才变为透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53310205/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com