Implement immersive status bar with toolbar and user-defined navigation bar respectively

1、 Toolbar

1. Add dependencies in build.gradle, for example:

compile 'com.android.support:appcompat-v7:23.4.0'

2. Remove the action bar of the application. You can change the theme theme to "noactionbar", for example:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

Or, instead of changing the theme to "noactionbar", under the style of the theme, add:

The second attribute represents whether to use actionbar instead of titlebar.

In fact, when I first learned, I was wondering why there was another titlebar? Later, it was found that before 3.0, the title bar (only a small amount of information such as title) was under the status bar. After 3.0, it became the application bar, that is, the actionbar.

In addition, when I test, the activity is inherited from appcompatactivity, and the theme is of appcompat type. In this case, you must write as above to have effect. If you write less or have different values, it will either have no effect or report an error.

Finally, the descriptions of the above two attributes can be viewed in the Android. R. attr class.

3. Add attributes to the toolbar in XML

Fitssystemwindows is the key for the toolbar to implement the immersive status bar. The general situation is that if it is set to true, the view will be adjusted to leave some space for the system window. If it is not set or set to false, the toolbar will overlap with the status bar.

In the second attribute, its value is written as "? Android: attr / actionbarsize", which means to refer to the actionbarsize attribute in the current topic. See the accessing resources section of the official document for more instructions.

The above two properties can be viewed in the android.view.view class.

4. Add the code to judge the SDK version in Java, and set the status bar to transparent when the user's system is 4.4 or above

This operation is the key to the implementation of the immersive status bar, whether it is the toolbar or the custom navigation bar.

Because setting the status bar to be transparent requires more than 4.4 to be used, systems below 4.4 cannot implement immersive status bars. In 4.4 to 5.0 systems, the status bar is fully transparent, that is, its color will be the same as that of your toolbar and custom navigation bar. In systems above 5.0, it is translucent, which will look darker.

When I tested on the 6.0 system, I found that this step was not set and set. From the effect, the difference is that the color of the status bar is lighter when it is not set, and the padding top of the toolbar is 0, while the color set is darker, and the padding top is the height of the status bar. The specific impact is unclear. However, this will deform the appearance of custom navigation. It will increase the height of the status bar, but it does not overlap with the status bar, resulting in deformation of the effect.

5. Finally, add in Java

setSupportActionBar(mToolbar);

Layout code of toolbar:

Rendering (Android 6.0):

2、 Customize navigation bar topbar

1. Set the window to untitled. Both methods in step 2 above can be implemented, or add the following code in Java:

requestWindowFeature(Window.FEATURE_NO_TITLE);

Note that when adding this code, make sure it is before loading the layout content, that is, before the setcontentview of oncreate. In the "Android control architecture" section of Android group biography, it explains why requestwindowfeature() needs to be preceded by setcontentview().

In addition, I found that if the activity inherits appcompatactivity, only the above code remains unchanged, and the actionbar is displayed. However, if you inherit fragmentactivity, it will have an effect. That is to say, in the second method in step 2 above, it is OK to add only any of the attributes. As for the reason, I haven't figured it out yet.

2. As in step 4 above, judge the system version and set the status bar as transparent as required

3. Gets the height of the status bar

This part of the code is implemented using Java's reflection mechanism. Because this internal package will be removed by default by android.jar in SDK / platforms / Android version, you can't directly call or view the classes in this package. If you want to use it, you can use this open source project https://github.com/anggrayudi/android-hidden-api 。

4. Get the height of the custom topbar and modify the layout parameters

In the include topbar layout file, the parent layout is LinearLayout, while the parent layout of the topbar is relativelayout, so it should be converted to ViewGroup first, and then to linearlayout.layoutparams when getlayoutparams is available.

Topbar layout:

Because the height of this layout will be dynamically modified in the code, that is, 49dp plus the height of the status bar, if there is only one hierarchical structure, the content of the navigation bar will be upward. Therefore, you should nest one more layer to maintain the height of the navigation bar. At the same time, add the attribute Android: gravity = "bottom" in the outermost layout to ensure that the navigation bar does not lean upward.

Rendering (Android 6.0):

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>