Why is my layout messed up after resetting the FLAG_TRANSLUCENT_STATUS flag?

I have one Activity application. It displays a LoginFragment , which makes the transparent status and navigation bars transparent so that it can display the background image behind it. However, after entering the system, this fragment is replaced by another fragment, which should display the usual solid status and navigation lines. Thus, before deleting LoginFragment it disables the set flags to make the bars opaque.

The problem that I encountered is that after entering the system, the fragment with the usual statuses and navigation panels has the action bar shifted down. If I turn my screen to the landscape and then return to the portrait to slow down the restoration of the layout, the action bar will return to the correct position.

When the status bar is translucent. It's fine. When the status bar is translucent, that's fine.

On the next screen, the action bar is shifted down. Also note that the status bar is black in color, it should be gray, according to the topic. On the next screen, the action bar is shifted down.

Now, if I go back to the first screen, the top translucent panel is fine, but all the content is pushed up, leaving an empty space below. Now, if I go back to the first screen, the top translucent panel is fine, but all the content is pushed up, leaving an empty empty space below.

Code in LoginFragment , which programmatically makes the strips translucent, and also restores them: -

 @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); } } 

Layout xml activities: -

 <?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 layout: -

 <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" /> 

+7
android android-layout android-fragments
source share
3 answers

I had the same problem before, so if someone came across this, the correct solution would be to set the NO_LIMITS flag:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); } 
+4
source share

I had a similar problem, and it looks like we should set the window flags before onActivityCreated (Bundle) . I often use static methods to display my fragments, for example:

 public class FragmentPlayer extends MediaFragment { public FragmentPlayer() { // Default public constructor required. } public static void show(@NonNull Activity activity, boolean compact) { boolean isOrientationPortrait = UiHelper.isOrientationPortrait(activity); setupWindow(activity.getWindow(), isOrientationPortrait, compact); FragmentManager fragmentManager = activity.getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); FragmentPlayer newFragment = FragmentPlayer.getInstance(compact); transaction.replace(R.id.fragment_player, newFragment); transaction.commit(); } private static void setupWindow(@NonNull Window window, final boolean isOrientationPortrait, final boolean compact) { if (compact) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } else { if (isOrientationPortrait) { window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } else { window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } } } 

As you can see, I moved all Window initialization from onCreateActivity(Bundle) to the static setupWindow(Window, boolean, boolean) method setupWindow(Window, boolean, boolean) , and now everything works fine.

I still don't know why the layout was actually messed up when calling these methods from onCreateActivity(Bundle) , but this is the beginning.

Edit 1: and don't forget to also configure Window in Activity.onCreate(Bundle) to handle screen rotations correctly!

+2
source share

I tried every opportunity to fix this problem and found a clear solution. First do what is shown in the youtube video and add android:fitsSystemWindows="true" in android.support.v7.widget.Toolbar in xml.

-one
source share

All Articles