【问题标题】:Transparent status bar overlaps action bar透明状态栏与操作栏重叠
【发布时间】:2017-02-21 21:57:49
【问题描述】:

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

预期结果: 操作栏Toolbar 的上边界应直接显示在状态栏下边界的下方,并且操作栏中的任何MenuItems 都应完全可见。

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

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

【问题讨论】:

  • 你能分享完整的布局文件吗?和布局预览图像?我在布局中看不到测试和跳过按钮代码。
  • 其他布局也会出现这种情况吗?
  • @rahul,也许 Ryan 以编程方式添加标题和菜单项,而不是在 xml 中
  • @ntaloventi 如果是这种情况,我认为制作自定义工具栏并将其包含在布局文件中将是一个好主意。它使代码更简洁,更易于维护。
  • @rahul,同意你的观点,但基于 Ryan 布局,没有你所期望的 testskip button

标签: android android-actionbar statusbar transparent


【解决方案1】:

我的问题是由于我将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"/>

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

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

【讨论】:

  • 对于需要layout_height = ?attr/actionBarSize 的正常大小操作栏的屏幕,不存在此解决方案。我还没有找到解决方案。 ://
  • 帮助我保持状态栏半透明(几乎与工具栏颜色相同)和大小合适的工具栏的原因是将所有活动内容放入 xml 中的协调器布局中。
  • 我已经与类似的问题斗争了一天,唯一对我有帮助的是在工具栏上设置 android:fitsSystemWindows="false" 如该答案stackoverflow.com/a/34856737/490114 中所建议的那样,然后大小和工具栏的填充设置正确
  • 谢谢,在搜索了一天的问题后,这很有帮助。工具栏中的属性 android:fitsSystemWindows="true" 成功了。它把工具栏推到了状态栏的正下方
【解决方案2】:

尝试将您的 Toolbar 移动到 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/>

【讨论】:

    【解决方案3】:

    我正在使用标志来全屏显示活动,我的风格是 NoActionBar。

    如果我将fitsSystemWindows=true 设置为工具或父布局,则活动内容大小会因导航栏和状态栏而改变。

    我将ToolBarAppBarLayout 包裹在Twinkie_Monkey's answer 中,然后我将fitsSystemWindows=true 设置为AppBarLayout 并且它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-12
      • 2015-07-06
      • 2011-02-20
      相关资源
      最近更新 更多