【问题标题】:Why is my BottomSheetDialogFragment ignoring my app theme?为什么我的 BottomSheetDialogFragment 忽略了我的应用主题?
【发布时间】:2021-09-19 16:21:27
【问题描述】:

我刚开始在我的应用程序中使用BottomSheetDialogFragment,但它似乎忽略了我的应用程序主题。我的应用程序使用深色主题,BottomSheetDialogFragment 以白色背景显示,并且不使用我的应用程序的强调色。这是唯一具有这种行为的 Android 组件。为什么会这样以及如何解决这个问题?

public class CustomBottomDialogFragment extends BottomSheetDialogFragment {

    public static CustomBottomDialogFragmentnewInstance(long id) {
        final CustomBottomDialogFragmentdialog = new CustomBottomDialogFragment();
        Bundle args = new Bundle();
        args.putLong(Keys.ARG1, id);
        dialog.setArguments(args);
        return dialog;
    }

  @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final long id = getArguments().getLong(Keys.ARG1);
        final boolean isLiveStream = PodcastHelper.isLiveStream(podcastId);
        final View view = inflater.inflate(R.layout.custom_bottom_sheet_layout, container, false);

...

        return view;
    }

【问题讨论】:

  • 展示你是如何实现底部表格的?

标签: android bottom-sheet bottomsheetdialogfragment


【解决方案1】:

BottomSheetDialogFragment 的默认主题不是应用的主题,而是Theme.Design.Light.BottomSheetDialog

他们的那个风格的资源是R.style.Theme_Design_Light_BottomSheetDialog,你可以清楚地从their class definition查看这个

  private static int getThemeResId(@NonNull Context context, int themeId) {
    if (themeId == 0) {
      // If the provided theme is 0, then retrieve the dialogTheme from our theme
      TypedValue outValue = new TypedValue();
      if (context.getTheme().resolveAttribute(R.attr.bottomSheetDialogTheme, outValue, true)) {
        themeId = outValue.resourceId;
      } else {
        // bottomSheetDialogTheme is not provided; we default to our light theme
        themeId = R.style.Theme_Design_Light_BottomSheetDialog;
      }
    }
    return themeId;
  }

因此,您需要将其更改为与应用的主题相匹配。 Here 是 github 上针对此问题提供的几个解决方案

假设您应用的主题是R.style.AppTheme

解决方案 1:

感谢Bloody-Badboy

在您的自定义 BottomSheetDialogFragment 中应用应用的主题:

 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    val contextThemeWrapper = ContextThemeWrapper(activity, R.style.AppTheme) // your app theme here
    return inflater.cloneInContext(contextThemeWrapper).inflate(R.layout.custom_bottom_sheet_layout, container, false)
  }

解决方案 2:

感谢DSteve595

覆盖上面提到的BottomSheetDialogFragment的默认bottomSheetDialogTheme样式:

<style name="AppTheme" parent=".....">
    <item name="bottomSheetDialogTheme">@style/ThemeOverlay.YourApp.BottomSheetDialog</item>
</style>

<style name="ThemeOverlay.YourApp.BottomSheetDialog" parent="ThemeOverlay.MaterialComponents.Dialog">
  <item name="android:windowBackground">@android:color/transparent</item>
  <item name="android:windowAnimationStyle">@style/Animation.MaterialComponents.BottomSheetDialog</item>
  <item name="bottomSheetStyle">@style/Widget.MaterialComponents.BottomSheet.Modal</item>
</style>

为了进一步研究,thismedium article 也会更清楚

【讨论】:

  • 第一个解决方案解决了我的问题。非常感谢!
猜你喜欢
  • 2011-12-31
  • 2013-09-30
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
相关资源
最近更新 更多