底部对话框片段主题与皮肤

5

如何将BottomSheetDialogFragment主题与其他主题结合?

我的应用程序使用主题制作皮肤。我想让BottomSheetDialogFragment显示为圆角矩形,我通过以下方式实现:

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme) /* hack to make background transparent */
 }

然后在styles.xml中:
<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@android:color/transparent</item>
</style>

<style name="CustomBottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item>
</style>

如果我从 Theme.MaterialComponents.Light.BottomSheetDialog 扩展,我就得不到我在皮肤主题中定义的颜色方案。

所以问题是:如何在皮肤主题内定义对话框主题?


你在 "setupDialog" 重写方法中设置了 BottomSheetDialogFragment 的任何视图吗? - android
肯定的,我会解析XML。实际上,我是在onCreateView方法中进行的。最终,我不得不将主题颜色复制到对话框主题中。看起来这是唯一的解决方案 :/ - Angelina
3个回答

8

您可以在应用程序主题中添加bottomSheetDialogTheme属性,以设置全局的bottomsheetDialog样式。

<style name="AppTheme" parent="Theme.MaterialComponents.*">
   ......
   <item name="bottomSheetDialogTheme">@style/BottomSheetDialog_Rounded</item>
</style>

<style name="BottomSheetDialog_Rounded" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/BottomSheet_Rounded</item>
</style>

否则,在您的BottomSheetDialogFragment中,您可以覆盖getTheme()方法。
public class RoundedBottomSheetDialog extends BottomSheetDialogFragment {

  //....

  @Override public int getTheme() {
    return R.style.BottomSheetDialog_Rounded;
  }
}

另外,如果要实现圆角效果,可以使用以下代码:

  <!-- BottomSheet Dialog-->
  <style name="BottomSheetDialog_Rounded" parent="@style/ThemeOverlay.MaterialComponents.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/BottomSheet_Rounded</item>
  </style>

  <style name="BottomSheet_Rounded" parent="Widget.MaterialComponents.BottomSheet">
    <item name="shapeAppearanceOverlay">@style/ShapeAppearanceBottomSheetDialog_Rounded</item>
  </style>

  <style name="ShapeAppearanceBottomSheetDialog_Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>

1
尝试使用这个代替:

kotlin

override fun getTheme(): Int = R.style.CustomBottomSheetDialogTheme

Java

@Override
public int getTheme() {
     return R.style.CustomBottomSheetDialogTheme
}

0
override fun onCreateDialog(@Nullable savedInstanceState: Bundle?): Dialog 
val dialog = BottomSheetDialog(context!!,R.style.FullScreenBottomSheet)

<style name="FullScreenBottomSheet" 
parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowFullscreen">false</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:statusBarColor">@color/transparent</item>
</style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接