简体   繁体   English

如何使用边到边手势导航实现系统栏插入?

[英]How to implement system bars insets with edge-to-edge gesture navigation?

I'm trying to add the edge-to-edge stuff for the gesture navigation bar to the Tip Time app from Google .我正在尝试将手势导航栏的边到边内容添加到Google 的 Tip Time 应用程序 I added the transparent navigationBarColor XML tag to themes.xml as well as the following code to the onCreate() function of MainActivity.kt :我将透明的navigationBarColor XML 标签添加到themes.xml中,并将以下代码添加到MainActivity.ktonCreate() function 中:

android边到边代码错误

This was directly copy-pasted from the documentation.这是直接从文档中复制粘贴的。 Android Studio says that "it cannot find a parameter with this name" for each of the three margins. Android Studio 表示对于三个边距中的每一个“它找不到具有此名称的参数”。 I noticed that changing the parenthesis right after <ViewGroup.MarginLayoutParams> to curly braces fixes the compiler error.我注意到将<ViewGroup.MarginLayoutParams>之后的括号更改为大括号可修复编译器错误。 Maybe the documentation is just wrong?也许文档是错误的?

Anyways, even after fixing that, the app still doesn't look right:无论如何,即使在修复之后,应用程序看起来仍然不正确:

android edge-to-edge 模拟器错误

As you can see, the entire view gets shifted up slightly and the "Cost of Service" TextView is partially cut-off by the app bar.如您所见,整个视图略微向上移动,“服务成本” TextView被应用栏部分截断。 What would I need to change to implement the system/navigation bar insets for edge-to-edge content so the UI looks nice?我需要更改什么才能实现边到边内容的系统/导航栏插入,以便 UI 看起来不错? Also, as a side-question, how can I change the dark blue color of the system status bar to match the color of the app bar so that they look blended?另外,作为附带问题,我如何更改系统状态栏的深蓝色以匹配应用栏的颜色,使它们看起来混合?

As per documentation for edge to edge contents :根据边到边内容的文档:

Draw behind the status bar if it makes sense for your content and layout, such as in the case of full-width imagery.如果它对您的内容和布局有意义,例如在全宽图像的情况下,请在状态栏后面绘制。 To do this, use APIs such as AppBarLayout, which defines an app bar pinned to the top of the screen.为此,请使用 AppBarLayout 等 API,它定义固定到屏幕顶部的应用栏。

So, while handing the window insets (especially the top one), you can't use the default ActionBar , instead you need to customize that with AppBarLayout and ToolBar , and to make it act as the ActionBar , use setSupportActionBar() , and a NoActionBar app theme;因此,在处理 window 插图(尤其是顶部的插图)时,您不能使用默认的ActionBar ,而是需要使用AppBarLayoutToolBar自定义它,并使其充当ActionBar ,使用setSupportActionBar()和一个NoActionBar应用主题; it'd be <style name="Theme.TipTime" parent="Theme.MaterialComponents.DayNight.NoActionBar"> in the shared repo.它是<style name="Theme.TipTime" parent="Theme.MaterialComponents.DayNight.NoActionBar">在共享仓库中。

the entire view gets shifted up slightly and the "Cost of Service" text field is partially cut-off by the app bar.整个视图略微向上移动,“服务成本”文本字段被应用栏部分截断。

The reason that the sample uses the default ActionBar instead of a customized one;示例使用默认ActionBar而不是自定义 ActionBar 的原因; when it comes to handle the top window insets, it won't affect the default ActionBar ;在处理顶部 window 插图时,它不会影响默认的ActionBar notice that you pass in the activity's root layout to setOnApplyWindowInsetsListener callback, and as the ActionBar is not a part of the activity, it won't be affected.请注意,您将活动的根布局传递给setOnApplyWindowInsetsListener回调,并且由于ActionBar不是活动的一部分,因此不会受到影响。 Therefore the activity is shifted up behind the ActionBar when the top inset is removed.因此,当删除顶部插图时,活动将向上移动到ActionBar后面。 So, to solve this you have either to totally remove the default ActionBar or to use a custom toolbar instead.因此,要解决此问题,您必须完全删除默认的ActionBar或改用自定义工具栏。

Also, as a side-question, how can I change the dark blue color of the system status bar to match the color of the app bar so that they look blended?另外,作为附带问题,我如何更改系统状态栏的深蓝色以匹配应用栏的颜色,使它们看起来混合?

Use a transparent status bar color in the app's theme:在应用的主题中使用透明的状态栏颜色:

<item name="android:statusBarColor" tools:targetApi="l">@android:color/transparent</item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM