1

我刚开始BottomSheetDialogFragment在我的应用程序中使用 a ,但它似乎忽略了我的应用程序主题。我的应用程序使用深色主题,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;
    }
4

1 回答 1

2

的默认主题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:

血腥坏小子的学分

在您的自定义中应用应用程序的主题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

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

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

为了进一步研究,这篇中篇文章也将使其更加清晰

于 2021-09-20T02:18:07.697 回答