【问题标题】:BottomSheetDialogFragment theme with skins带有皮肤的 BottomSheetDialogFragment 主题
【发布时间】:2019-04-26 10:37:46
【问题描述】:

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 扩展,我不会得到我在皮肤主题中定义的配色方案。

那么问题来了:如何在皮肤主题中定义Dialog主题?

【问题讨论】:

  • 您是否在“setupDialog”覆盖方法中设置BottomSheetDialogFragment中的任何视图?
  • 当然,我膨胀了 XML。我实际上是在 onCreateView 中完成的。最后,我最终将主题中的颜色复制粘贴到对话框主题中。似乎是唯一的解决方案:/

标签: android android-theme bottom-sheet material-components material-components-android


【解决方案1】:

您可以在您的应用主题中添加 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>

【讨论】:

    【解决方案2】:

    试试这个

    科特林

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

    java

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

    【讨论】:

      【解决方案3】:
      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-24
        相关资源
        最近更新 更多