use*_*605 4 android android-theme bottom-sheet bottomsheetdialogfragment
我刚刚开始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;
}
Run Code Online (Sandbox Code Playgroud)
默认主题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;
}
Run Code Online (Sandbox Code Playgroud)
因此,您需要更改它以与应用程序的主题相匹配。以下是 github 上针对此问题提供的几个解决方案
假设您的应用程序的主题是R.style.AppTheme:
解决方案一:
致血腥坏男孩的致谢
在您的自定义中应用应用程序的主题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)
}
Run Code Online (Sandbox Code Playgroud)
解决方案2:
覆盖上面提到的默认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>
Run Code Online (Sandbox Code Playgroud)
为了进一步研究,这篇媒体文章也会使它更清楚
| 归档时间: |
|
| 查看次数: |
2443 次 |
| 最近记录: |