【发布时间】: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>
操作栏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 视图之前,状态栏仍然覆盖在操作栏上,但“SKIP”MenuItem 在操作栏中垂直居中,导致它部分显示在状态栏下方.我希望fitsSystemWindows=true 标志能给我我预期的结果(如上所述),但它没有。就好像fitsSystemWindows="true" 正确定位了“跳过”按钮,但没有调整操作栏本身的位置。有人知道这里可能是什么问题吗?
编辑:我意识到我可以删除 fitsSystemWindows="true" 并将 marginTop="...statusBarHeight" 添加到 Toolbar 视图,但我正在寻找一种更清洁的方法来解决这个问题。
【问题讨论】:
-
你能分享完整的布局文件吗?和布局预览图像?我在布局中看不到测试和跳过按钮代码。
-
其他布局也会出现这种情况吗?
-
@rahul,也许 Ryan 以编程方式添加标题和菜单项,而不是在 xml 中
-
@ntaloventi 如果是这种情况,我认为制作自定义工具栏并将其包含在布局文件中将是一个好主意。它使代码更简洁,更易于维护。
-
@rahul,同意你的观点,但基于 Ryan 布局,没有你所期望的
test和skip button
标签: android android-actionbar statusbar transparent