透明状态栏与操作栏重叠

Rya*_*yan 4 android transparent statusbar android-actionbar

实际结果:状态栏显示在操作栏ToolbarMenuItem操作栏里面被切断。注意:“测试”是操作栏的title.

JankActionBar

预期结果:操作栏的顶部边界Toolbar应直接出现在状态栏的底部边界下方,并且MenuItem操作栏中的任何s 都应完全可见。

活动的 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>
Run Code Online (Sandbox Code Playgroud)

操作栏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;
}
Run Code Online (Sandbox Code Playgroud)

在我添加fitsSystemWindows="true"Toolbar视图之前,状态栏仍然覆盖在操作栏上,但“跳过”MenuItem在操作栏中垂直居中,导致它部分出现在状态栏下方。我希望fitsSystemWindows=true标志会给我我预期的结果(如上所述),但它没有。就好像fitsSystemWindows="true"正确定位了“跳过”按钮,但没有调整操作栏本身的位置。有谁知道这里可能是什么问题?

编辑:我意识到我可以删除fitsSystemWindows="true"并添加marginTop="...statusBarHeight"Toolbar视图中,但我正在寻找一种更清洁的方法来解决这个问题。

Rya*_*yan 7

我的问题是由于我将Toolbar's设置layout_height?attr/actionBarSize. 我最初认为fitsSystemWindows重新定位Toolbar,但它似乎添加了一个填充。因此,当将顶部填充添加到 时ToolbarToolbar的内容会被下推。由于Toolbar的高度是固定的,内容被推到容器的下限之下。我最终设置?attr/actionBarSizeToolbar'sminHeight属性的值来解决这个问题。以下是我的更新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"/>
Run Code Online (Sandbox Code Playgroud)

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

巨大的ActionBar

警告更新:好的。很明显,我的操作栏收到了额外的底部填充,相当于导航栏的高度,因为我设置<item name="android:windowTranslucentNavigation">true</item>了我Activity的主题。我通过删除windowTranslucentNavigation属性验证了这一点。我正在使用 Android 支持库 v25.1.1 在 7.1 像素上对此进行测试。