为什么我的BottomSheetDialogFragment忽略了我的应用程序主题?

4

我刚开始在我的应用中使用了一个 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;
    }

请展示一下您如何实现底部工作表? - philoopher97
1个回答

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

该样式主题的资源是 R.style.Theme_Design_Light_BottomSheetDialog,您可以从 它们的类定义 中清楚地查看这一点。

  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;
  }

所以,您需要更改此内容以与应用程序的主题相匹配。这里提供了几个在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>

为了进一步研究,这篇中等文章也会使它更加清晰明了。


1
第一个解决方案解决了我的问题。非常感谢! - user1026605
对我来说,只有第一个解决方案有效,而第二个则无效。@user1026605,你也是这样吗? 尽管这个问题在Google的问题追踪器中已经解决,但问题仍然存在,我们不得不使用第一个解决方法。 - AndroidEngineX
第二个解决方案解决了我的问题。非常感谢。 - undefined

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