[英]Transparent status bar overlaps action bar
实际结果:状态栏出现在操作栏Toolbar
上方,操作栏内的MenuItem
被截断。 注意:“测试”是操作栏的title
。
预期结果:操作栏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>
操作栏title
和MenuItem
是在运行时添加的。
@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
视图之前,状态栏仍然覆盖操作栏,但“跳过” MenuItem
项在操作栏中垂直居中,导致它部分出现在状态栏下方。 我希望fitsSystemWindows=true
标志能给我预期的结果(如上所述),但它没有。 就好像fitsSystemWindows="true"
正确定位了“跳过”按钮,但没有调整操作栏本身的位置。 任何人都知道这里可能是什么问题?
编辑:我意识到我可以删除fitsSystemWindows="true"
并将marginTop="...statusBarHeight"
添加到Toolbar
视图,但我正在寻找更简洁的方法来解决这个问题。
我的问题是由于我将Toolbar
的layout_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"/>
警告:如果您不想在操作栏正下方显示任何内容,则此方法有效,因为操作栏的高度出于某种原因大约是包含一行主页图标所需高度的两倍,标题和/或菜单项。 如果有人知道实现非重叠状态栏和正常大小操作栏的方法,请分享您的见解。 我将永远感激不已。
警告更新:好的。 很明显,我的操作栏收到了额外的底部填充,相当于导航栏的高度,因为我在Activity
的主题上设置了<item name="android:windowTranslucentNavigation">true</item>
。 我通过删除windowTranslucentNavigation
属性验证了这一点。 我正在使用 Android 支持库 v25.1.1 在 7.1 Pixel 上对此进行测试。
尝试将您的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/>
我使用标志使活动全屏显示,我的风格是 NoActionBar。
如果我将fitsSystemWindows=true
设置为工具布局或父布局,活动内容大小会因导航栏和状态栏而改变。
我像Twinkie_Monkey 的回答一样用AppBarLayout
包装了ToolBar
,我将fitsSystemWindows=true
设置为AppBarLayout
并且它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.