android - 透明状态栏与操作栏重叠

标签 android android-actionbar statusbar transparent

实际效果:状态栏出现在操作栏Toolbar上方,操作栏内的MenuItem被截断。注意:“测试”是操作栏的标题

JankActionBar

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

Activity 的 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 View 之前,状态栏仍然覆盖操作栏,但是 'SKIP' MenuItem 在操作栏中垂直居中,导致它部分出现在状态栏下方。我希望 fitsSystemWindows=true 标志能给我预期的结果(如上所述),但它没有。就好像 fitsSystemWindows="true" 正确定位了“跳过”按钮但没有调整操作栏本身的位置。有人知道这里可能是什么问题吗?

编辑:我意识到我可以删除 fitsSystemWindows="true" 并将 marginTop="...statusBarHeight" 添加到 Toolbar 查看,但我正在寻找更简洁的方法来解决此问题。

最佳答案

我的问题是因为我设置了 Toolbarlayout_height?attr/actionBarSize .我本来以为fitsSystemWindows重新定位 Toolbar ,但它似乎改为添加填充。因此,当将顶部填充添加到 Toolbar 时, Toolbar的内容被下推。自 Toolbar的高度是固定的,内容被推到容器的下限以下。我最终设置了 ?attr/actionBarSize作为 Toolbar 的值的 minHeight属性来解决这个问题。以下是我更新的 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

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

关于android - 透明状态栏与操作栏重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42378765/

相关文章:

android - 如何/我应该实现卡尔曼滤波器以获得准确的加速度计数据?

java - ArrayAdapter<MyClass> 附加到 View

android - 更改 ActionBar 上溢出按钮的颜色

Android 操作按钮保留以前的 View

android - 如何检查 Android 中的状态栏颜色?

java - 隐藏/显示带有凹口的状态栏

Android Uri.Builder 在方案后使用 "/"而不是 "//"

android - 从位图中绘制圆形区域

java - Android 弹出菜单未锚定到操作栏上的按钮

android - 在 Android 中更改为全屏时如何删除状态栏动画