Android 4.4 immersive transparent status bar and navigation bar, android4.4
The UI of the Android system has not changed much since 4.2, and 4.4 Only adds the transparent status bar and navigation bar function,
Now I will explain how to use this new feature so that your app can follow the trend. Of course, if you don't care about the appearance,
This feature can be used to develop a very beautiful UI. Especially for google's native system, the navigation bar at the bottom of the screen occupies a piece of screen space, and it looks uncomfortable.
Okay. Let's talk about the technology. The first method is to set it in the Code:
- If (VERSION. SDK_INT> = VERSION_CODES.KITKAT ){
- // Transparent Status Bar
- GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_TRANSLUCENT_STATUS );
- // Transparent navigation bar
- GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_TRANSLUCENT_NAVIGATION );
- }
Directly calling the above two lines of code can be transparent, but you will find that your view goes to the actionbar. Obviously, google's intention is to make your view occupy the entire screen, then the status bar and navigation bar are transparently covered above, which is obviously not feasible.
Is there any way to keep your view in the original size?
Yes. You need to add two attributes to the layout xml file of the activity.
- <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
- Android: layout_width = "fill_parent"
- Android: layout_height = "fill_parent"
- Android: gravity = "center_horizontal"
- Android: fitsSystemWindows = "true"
- Android: clipToPadding = "true"
- Android: orientation = "vertical">
In this way, the background of the status bar is the main background of your activity. If the actionbar is present, it will be ugly,
Facts have proved that google does not provide a better solution. Its transparent status bar and navigation bar are limited to scenarios such as reading text in full screen or playing games,
The second method is to set the theme attribute.
- Android: theme = "@ android: style/Theme. DeviceDefault. Light. NoActionBar. TranslucentDecor"
- Android: theme = "@ android: style/Theme. Holo. Light. NoActionBar. TranslucentDecor"
- Android: theme = "@ android: style/Theme. Holo. NoActionBar. TranslucentDecor"
Copy the Code. If you use a custom topic, you only need to add the following attributes to the values-19 file:
- <Style name = "AppBaseTheme" parent = "android: Theme. Holo. Light. DarkActionBar">
- <! -- API 19 theme customizations can go here. -->
- <Item name = "android: windowTranslucentStatus"> true </item>
- <Item name = "android: windowTranslucentNavigation"> true </item>
- </Style>
Copy code
I just mentioned that this application has limitations, but fortunately there is an open source thing.
Https://github.com/jgilfelt/SystemBarTint
To use this open-source library, you must enable the transparent title bar.
Use this topic
- <Style name = "AppBaseTheme" parent = "android: Theme. Holo. Light. DarkActionBar">
- <! -- API 19 theme customizations can go here. -->
- <Item name = "android: windowTranslucentStatus"> true </item>
- <Item name = "android: windowTranslucentNavigation"> true </item>
- </Style>
Or call this code before setContentView.
- If (VERSION. SDK_INT> = VERSION_CODES.KITKAT ){
- // Transparent Status Bar
- GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_TRANSLUCENT_STATUS );
- // Transparent navigation bar
- GetWindow (). addFlags (WindowManager. LayoutParams. FLAG_TRANSLUCENT_NAVIGATION );
- }