防止BottomSheetDialogFragment覆盖导航栏 [英] Prevent BottomSheetDialogFragment covering navigation bar
问题描述
我使用的是非常幼稚的代码来显示一个底页对话框片段:
I'm using really naive code to show a bottom sheet dialog fragment:
class LogoutBottomSheetFragment : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.view_image_source_chooser, container, false)
return view
}
}
这是我叫这个对话框的方式:
This is how I called this dialog:
LogoutBottomSheetFragment().show(supportFragmentManager, "logout")
但是我在下图中看到了这个可怕的地方. 如何保持导航栏为白色(后退/主页软件按钮所在的底部栏)?
But I get this horrible shown in the image below. How can I keep the navigation bar white (the bottom bar where the back/home software buttons are)?
我正在使用的应用主题:
App Theme I'm using:
<!-- Base application theme. -->
<style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style
<style name="AppTheme" parent="BaseAppTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<!-- Main theme colors -->
<!-- your app branding color for the app bar -->
<item name="android:colorPrimary">@color/colorPrimary</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="android:colorPrimaryDark">@android:color/white</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="android:colorAccent">@color/charcoal_grey</item>
<item name="colorControlNormal">@color/charcoal_grey</item>
<item name="colorControlActivated">@color/charcoal_grey</item>
<item name="colorControlHighlight">@color/charcoal_grey</item>
<item name="android:textColorPrimary">@color/charcoal_grey</item>
<item name="android:textColor">@color/charcoal_grey</item>
<item name="android:windowBackground">@color/white</item>
</style>
我也尝试覆盖setupDialog而不是onCreateView,但仍然会发生:
I've also tried to override the setupDialog instead of the onCreateView, but still happens:
@SuppressLint("RestrictedApi")
override fun setupDialog(dialog: Dialog, style: Int) {
super.setupDialog(dialog, style)
val view = View.inflate(context, R.layout. view_image_source_chooser,null)
dialog.setContentView(view)
}
推荐答案
我有相同的问题,我终于找到了一个解决方案,该解决方案不是hacky或需要大量的代码.
I had the same problem and I finally found a solution which is not hacky or needs an orbitant amount of code.
此方法用LayerDrawable替换了窗口背景,该图层包含两个元素:背景暗淡和导航栏背景.
This Method replaced the window background with a LayerDrawable which consists of two elements: the background dim and the navigation bar background.
@RequiresApi(api = Build.VERSION_CODES.M)
private void setWhiteNavigationBar(@NonNull Dialog dialog) {
Window window = dialog.getWindow();
if (window != null) {
DisplayMetrics metrics = new DisplayMetrics();
window.getWindowManager().getDefaultDisplay().getMetrics(metrics);
GradientDrawable dimDrawable = new GradientDrawable();
// ...customize your dim effect here
GradientDrawable navigationBarDrawable = new GradientDrawable();
navigationBarDrawable.setShape(GradientDrawable.RECTANGLE);
navigationBarDrawable.setColor(Color.WHITE);
Drawable[] layers = {dimDrawable, navigationBarDrawable};
LayerDrawable windowBackground = new LayerDrawable(layers);
windowBackground.setLayerInsetTop(1, metrics.heightPixels);
window.setBackgroundDrawable(windowBackground);
}
}
方法"setLayerInsetTop"需要API 23,但那很好,因为在Android O(API 26)中引入了深色的导航栏图标.
The method "setLayerInsetTop" requieres the API 23 but thats fine because dark navigation bar icons were introduced in Android O (API 26).
因此,解决方案的最后一部分是像这样从底部的onCreate方法调用此方法.
So the last part of the solution is to call this method from your bottom sheets onCreate method like this.
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setWhiteNavigationBar(dialog);
}
return dialog;
}
我希望它能对您有所帮助,如果您发现无法解决此问题的设备或案例,请告诉我.
这篇关于防止BottomSheetDialogFragment覆盖导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!