实际结果:状态栏显示在操作栏Toolbar
上方,并且操作栏内的MenuItem
被切断。注意:“测试”是操作栏的title
。透明状态栏与操作栏重叠
预期结果: 绑定操作栏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>
操作栏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"
正确定位了“SKIP”按钮,但没有调整操作栏本身的位置。任何人都知道这可能是什么问题?
编辑:我意识到我可以删除fitsSystemWindows="true"
并将marginTop="...statusBarHeight"
添加到Toolbar
视图,但我正在寻找一种更简洁的方式来解决此问题。
可以请你分享完整的布局文件吗?和布局预览图像?我不能在布局中看到测试和跳过按钮代码。 – rahul
这是否发生在其他布局? –
@rahul,也许Ryan以编程方式添加标题和菜单项,而不是xml –