我有一个Activity应用程序.它显示一个LoginFragment,它使状态和导航条变为半透明,以便它可以在其后面显示背景图像.但是,登录后,此片段将被另一个需要显示常用固态和导航栏的片段替换.因此,在删除LoginFragment之前,它会取消设置的标志,以使条形不透明.
我面临的问题是,在我登录后,具有正常状态和导航栏的片段将其操作栏向下移位.如果我将屏幕旋转到横向然后再回到纵向以强制重新布局,则操作栏会快速回到正确的位置.
状态栏是半透明的.这很好.
在下一个屏幕上,操作栏向下移动.另外请注意,根据主题,状态栏是黑色的,应该是灰色的.
现在,如果我回到第一个屏幕,顶部的半透明条形图很好,但整个内容向上移动,在下面留下一个空白的空白区域.
LoginFragment中的代码,它以编程方式使条形变为半透明并恢复它们: –
@Override
public void onResume() {
super.onResume();
if (hasTransparentStatusBar()) {
setStatusBarTranslucent();
}
}
@Override
public void onPause() {
if (hasTransparentStatusBar()) {
setStatusBarOpaque();
}
super.onPause();
}
protected boolean hasTransparentStatusBar() {
return true;
}
protected void setStatusBarTranslucent() {
if (Build.VERSION.SDK_INT >= 21) {
Window window = getActivity().getwindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYstem_BAR_BACKGROUNDS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
protected void setStatusBarOpaque() {
if (Build.VERSION.SDK_INT >= 21) {
Window window = getActivity().getwindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYstem_BAR_BACKGROUNDS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
布局xml的活动: –
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.MainActivity" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/divider"
android:dividerHeight="0dp"
android:background="@color/primary"
/>
</android.support.v4.widget.DrawerLayout>
LoginFragment的布局xml: –
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="@drawable/logo"
android:id="@+id/logo"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:padding="@dimen/default_margin"
android:text="Login using Facebook"
android:textColor="@android:color/white"
android:background="@drawable/com_facebook_button_background"
android:id="@+id/authButton"
android:layout_below="@+id/logo"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
解决方法
我以前遇到过同样的问题,所以如果有人发现这个问题,正确的解决方法是设置NO_LIMITS标志:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getwindow();
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}