对于API 21及以上版本,内容会隐藏在软件导航栏后面

5

已经升级应用程序以使用Material Design - Theme.AppCompat.Light.NoActionBarToolbar替代ActionBar等。

但是遇到了一个问题。在API >= 21的设备上,底部内容会被软件导航栏覆盖隐藏(见下图)。

已经找到解决方法:

values-v21/styles.xml中:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="colorPrimaryDark">@color/green</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
</styles>

如果选项 <item name="android:windowDrawsSystemBarBackgrounds">false</item> - 底部内容可见,但状态栏会变成完全黑色。我无法将颜色更改为colorPrimaryDark(在我的情况下是绿色)。
如果选项 <item name="android:windowDrawsSystemBarBackgrounds">true</item> - 底部内容不可见,并且状态栏是绿色的,如预期。
我想要有一个颜色(绿色)的状态栏和可见的底部内容...可能与工具栏有关。它是否向下推内容?
有什么建议吗? <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowDrawsSystemBarBackgrounds">false</item> 更新:
如@azizbekian建议的那样,我将片段容器替换为CoordinatorLayout(在FrameLayout之前)并应用了android:fitsSystemWindows="true" 在这种情况下,底部面板可见,但不在底部.. 目标是保持按钮在底部...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:orientation="vertical">
    <include
        layout="@layout/toolbar"/>

        <!-- The main content view -->
        <android.support.design.widget.CoordinatorLayout
                android:id="@+id/content"
            android:fitsSystemWindows="true"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
</LinearLayout>

屏幕布局:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <FocusableScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/order_editor_layout"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <include
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/o_e_content" 
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true"/>
        </RelativeLayout>
    </FocusableScrollView>
    <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            layout="@layout/oe_bottom_pane"/>
</LinearLayout>

这是结果:

更新 #2

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ActionBarTheme"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <!-- The main content view -->
        <FrameLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>

替换了根活动的LinearLayoutCoordinatorLayout。 将FrameLayout作为内容的根元素。 将android:fitsSystemWindows =“true” 应用于CoordinatorLayout。 这样,所有内容都被轻微地向上移动,并且部分内容位于工具栏下方(您可以在下面的图片中看到 - 顶部元素是带有+和-符号的圆形。但是在以前的图片中,顶部有文本。)关于底部元素(按钮面板) - 仍然位于导航栏下方,但也略微向上移动。我已经标记了android:background =“@color/red”以更容易地识别此面板的位置。

看起来我们走在正确的路上。我们需要解决的问题是为什么内容移到了工具栏下面...如果工具栏成为顶部UI元素,那么按钮就会可见..


在你的layout.xml父布局中添加以下属性: android:fitsSystemWindows="true" - Andy Developer
1
尝试过这种方法,但它没有帮助我。现在,在某些地方底部内容是可见的,在某些地方则不可见。并且按钮不位于屏幕底部。我已经更新了我的问题。 - vsvydenko
1个回答

2

android:fitsSystemWindows="true"应用于您的根视图。

有关详细说明,请参见此帖子


在顶部我有一个Activity。使用FrameLayout作为片段的容器:<!-- 主要内容视图 --> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" />根据您的帖子 - 我所需要的 - 就是将FrameLayout更改为一些Materialish布局,如CoordinatorLayout,并应用fitsSystemWindows选项.. 我是对的吗? - vsvydenko
1
是的,还要将 android:windowDrawsSystemBarBackgrounds 设置为 true - azizbekian
我不确定在作为片段容器时使用CoordinatorLayout是否是一个好的实践。大多数情况下是FrameLayout。 - vsvydenko
@vsvydenko,移除LinearLayout。将CoordinatorLayout作为您的视图层次结构的根。目前,fitsSystemWindows不会生效,因为如帖子中所述,LinearLayout不会将系统插入分派到CoordinatorLayout - azizbekian
更新了我的问题。将CoordinatorLayout添加为顶部元素。我的一些内容移动到了工具栏下面。似乎我们需要解决这个问题,底部按钮面板将会可见。你对此有什么想法吗? - vsvydenko
显示剩余4条评论

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