透明状态栏重叠了操作栏。

4
实际结果:状态栏出现在操作栏Toolbar上方,操作栏中的MenuItem被裁剪了。注意:“Test”是操作栏的title

JankActionBar

期望结果:操作栏Toolbar的顶部边界应直接出现在状态栏底部边界下方,并且操作栏中的任何MenuItem都应完全可见。 活动的XML布局:
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/background"
        tools:ignore="ContentDescription"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

</FrameLayout>

运行时添加了操作栏titleMenuItem

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);

    setSupportActionBar((Toolbar) findViewById(R.id.action_bar));
    ActionBar actionBar = getSupportActionBar();
    assert actionBar != null;
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setTitle("Test");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_test, menu);
    return true;
}

在我将fitsSystemWindows =“true”添加到Toolbar视图之前,状态栏仍会覆盖操作栏,但“SKIP” MenuItem在操作栏中垂直居中,导致它部分出现在状态栏下方。我期望fitsSystemWindows = true标志可以给我预期的结果(如上所述),但事实并非如此。好像fitsSystemWindows =“true”正确定位了“跳过”按钮,但没有调整操作栏本身的位置。有人知道这里可能有什么问题吗?
编辑:我意识到我可以删除fitsSystemWindows =“true”并添加marginTop =“... statusBarHeight”Toolbar 视图,但是我正在寻找更清洁的解决方法。

请问您能否分享完整的布局文件和预览图像?我在布局中找不到“测试”和“跳过”按钮的代码。 - rahul
这个问题发生在其他布局上吗? - Iqbal Rizky
@rahul,也许Ryan通过编程方式添加标题和菜单项,而非在xml文件中添加。 - Iqbal Rizky
如果是这种情况,我认为制作一个自定义工具栏并将其包含在布局文件中是个好主意。这样可以使代码更加清晰易懂,易于维护。 - rahul
@rahul 刚刚添加了我在运行时使用的代码,以将标题和跳过按钮添加到操作栏中。我分享的布局是完整的布局。其余的屏幕截图只是“image_background” ImageView。在操作栏下面没有其他视图。 - Ryan
显示剩余3条评论
3个回答

7
我的问题是由于我将Toolbarlayout_height设置为?attr/actionBarSize导致的。我最初认为fitsSystemWindows会重新定位Toolbar,但实际上它只会添加填充。因此,当顶部填充被添加到Toolbar时,Toolbar的内容会被向下推送。由于Toolbar的高度是固定的,内容会被推到容器的下界之下。我最终将?attr/actionBarSize设置为ToolbarminHeight属性的值来解决这个问题。下面是我更新后的Toolbar
<android.support.v7.widget.Toolbar
    android:id="@+id/action_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

注意:如果您不想在操作栏直接下方显示任何内容,则此方法可行,因为由于某种原因,操作栏的高度大约是容纳主页图标、标题和/或菜单项所需的高度的两倍。如果有人知道如何实现非重叠状态栏和普通大小的操作栏,请分享您的见解。我将永远感激。

HugeActionBar

注意更新:好的。所以显然我的操作栏因为我在Activity的主题上设置了<item name="android:windowTranslucentNavigation">true</item>,而收到了额外的底部填充,其等于导航栏的高度。我通过删除windowTranslucentNavigation属性来验证这一点。我正在使用Android Support Libraries v25.1.1在7.1像素上进行测试。


1
这个解决方案并不适用于需要普通大小操作栏的屏幕,其layout_height = ?attr/actionBarSize。我还没有找到解决方案。 :/ - Ryan
让状态栏保持半透明(与工具栏几乎相同的颜色),并且正确大小的工具栏,帮助我的方法是将活动的所有内容放入XML中的协调布局中。 - shtolik
谢谢,经过一天的搜索后,这很有帮助。在工具栏中使用属性android:fitsSystemWindows="true"解决了问题。它将工具栏推到状态栏下方。 - mut tony

0

我正在使用标志将活动设置为全屏,我的样式是NoActionBar。

如果我将fitsSystemWindows=true设置为工具栏或父布局,则由于导航栏和状态栏的原因,活动内容大小会发生变化。

我像Twinkie_Monkey's answer中那样用AppBarLayout包装了ToolBar,并将fitsSystemWindows=true设置为AppBarLayout,它起作用了。


0
尝试将您的工具栏移动到AppBarLayout中。

像这样:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

 <android.support.v7.widget.Toolbar
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<AppBarLayout/>

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