带皮肤的BottomSheetDialogFragment主题

时间:2019-04-26 10:37:47

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

如何将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主题?

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

试试看

kotlin

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

java

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

答案 2 :(得分:0)

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