├── .gitignore ├── CHANGE_LOG.md ├── LICENSE.md ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img ├── 3.png ├── 4.png ├── DRIVE (2).png ├── Opera Instantánea_2018-05-15_005533_github.com.png ├── set_color.png ├── set_color_for_drawer_layout.png ├── set_color_for_swipe_back_page.png ├── set_for_image_view_page.png ├── set_translucnet.png ├── set_transparent.png ├── status_bar_util.png ├── switch_light_mode.jpeg └── use_in_fragment.gif ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── library │ │ └── ApplicationTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jaeger │ │ └── library │ │ └── StatusBarUtil.java │ └── res │ └── values │ ├── ids.xml │ └── strings.xml ├── sample ├── .gitignore ├── build.gradle ├── proguard-rules.pro ├── release │ ├── output.json │ └── sample-release.apk └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jaeger │ │ └── statusbarutil │ │ ├── BaseActivity.java │ │ ├── ColorStatusBarActivity.java │ │ ├── ImageFragment.java │ │ ├── ImageStatusBarActivity.java │ │ ├── ImageViewActivity.java │ │ ├── MainActivity.java │ │ ├── SimpleFragment.java │ │ ├── SwipeBackActivity.java │ │ ├── SwitchModeActivity.java │ │ └── UseInFragmentActivity.java │ └── res │ ├── anim │ ├── slide_in_left.xml │ ├── slide_in_right.xml │ ├── slide_out_left.xml │ └── slide_out_right.xml │ ├── drawable │ ├── bg_girl.jpg │ ├── bg_monkey.jpg │ ├── bg_monkey_king.jpg │ ├── ic_avatar.png │ ├── ic_favorite.png │ ├── ic_gavel.png │ ├── ic_grade.png │ ├── ic_group_work.png │ ├── ic_menu_camera.xml │ ├── ic_menu_gallery.xml │ ├── ic_menu_manage.xml │ ├── ic_menu_send.xml │ ├── ic_menu_share.xml │ ├── ic_menu_slideshow.xml │ └── nav_header_bg.jpg │ ├── layout │ ├── activity_color_status_bar.xml │ ├── activity_image_status_bar.xml │ ├── activity_image_view.xml │ ├── activity_main.xml │ ├── activity_switch_mode.xml │ ├── activity_use_in_fragment.xml │ ├── fragement_image.xml │ ├── fragement_simple.xml │ ├── nav_header.xml │ ├── sss.xml │ └── swipe_back_activity.xml │ ├── menu │ ├── activity_main_drawer.xml │ └── bottom_bar_items.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-v19 │ └── dimens.xml │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .idea -------------------------------------------------------------------------------- /CHANGE_LOG.md: -------------------------------------------------------------------------------- 1 | ## Change Log 2 | 3 | ### 1.5.1 4 | 5 | fix set light/dark mode bug 6 | 7 | ### 1.5.0 8 | 9 | add set light/dark mode method 10 | 11 | ### 1.4.0 12 | 13 | fix typo 14 | 15 | ### 1.3.6 16 | 17 | - bug fix and remove StatusBarView 18 | 19 | ### 1.3.5 20 | 21 | - add method `hideFakeStatusBarView` to hide fake StatusBarView 22 | 23 | ### 1.3.4 24 | ### 1.3.3 25 | 26 | - fix `setColorForSwipeBack` method with `CoordinatorLayout` bug 27 | 28 | ### 1.3.2 29 | 30 | - Bug fix 31 | 32 | ### 1.3.1 33 | 34 | - Bug fix 35 | 36 | ### 1.3.0 37 | 38 | - Fix multi-fragment problem && rootView may not ViewGroup bug 39 | 40 | #### 1.2.8 41 | #### 42 | - fix method `setColorDiff` bug 43 | 44 | ### 1.2.7 45 | 46 | - add `setColorForSwipeBack` method, now support 47 | 48 | - [r0adkll/Slidr: Easily add slide to dismiss functionality to an Activity](https://github.com/r0adkll/Slidr) 49 | 50 | ### 1.2.6 51 | 52 | - update support version to 24.2.1 53 | 54 | ### 1.2.5 55 | 56 | - add `@ColorInt`, if you pass a color res, now you will get a error 57 | 58 | - fix bug: ContentView in DrawerLayout padding not work 59 | 60 | ### 1.2.4 61 | 62 | - fix bug in method `setTransparentForImageViewInFragment` 63 | 64 | ### 1.2.3 65 | 66 | - fix method `setTranslucentForImageView` support pass null as needOffsetView 67 | 68 | - add method for fragment witch using ImageView as head view 69 | 70 | ~~~ java 71 | setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) 72 | setTransparentForImageViewInFragment(Activity activity, View needOffsetView) 73 | ~~~ 74 | 75 | - fix setTranslucent bug when root layout is CoordinatorLayout 76 | 77 | ~~~ java 78 | setTranslucentForCoordinatorLayout(Activity activity, int statusBarAlpha) 79 | ~~~ 80 | 81 | ### 1.2.0 82 | 83 | - add method for page witch using ImageView as head view 84 | 85 | ~~~ java 86 | setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView) 87 | ~~~ 88 | 89 | and 90 | 91 | ~~~ java 92 | setTranslucentForImageView(Activity activity, View needOffsetView) 93 | ~~~ 94 | 95 | ### 1.1.1 96 | 97 | - fix set color bug on 4.4 devices 98 | 99 | ### 1.1.0 100 | 101 | - fix some bugs 102 | 103 | ### 1.0.0 104 | 105 | - first publish 106 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StatusBarUtil 2 | 3 | [![996.icu](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu) 4 | 5 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-StatusBarUtil-green.svg?style=true)](https://android-arsenal.com/details/1/3341) 6 | 7 | [ ![Download](https://api.bintray.com/packages/laobie/maven/StatusBarUtil/images/download.svg) ](https://bintray.com/laobie/maven/StatusBarUtil/_latestVersion) 8 | 9 | A util for setting status bar style on Android App. It can work above API 19(KitKat 4.4). 10 | 11 | 12 | [中文版点我](http://laobie.github.io/android/2016/03/27/statusbar-util.html) 13 | 14 | ![](https://s9.postimg.cc/3p0itrz67/DRIVE_2.png) 15 | 16 | ### Sample 17 | 18 | [Download StatusBarUtil-Demo](http://fir.im/5mnp) 19 | 20 | ### ChangeLog 21 | 22 | [CLICK ME](CHANGE_LOG.md) 23 | 24 | ### Usage 25 | 26 | 1. Add the dependencies to your build.gradle file, StatusBarUtil is avaiable in JCenter: 27 | 28 | ```groovy 29 | compile 'com.jaeger.statusbarutil:library:1.5.1' 30 | ``` 31 | 32 | I fixed typo, change "statusbaruitl" to "statusbarutil", please notice this. 33 | 34 | 2. Call method you need after `setContentView()`, such as : 35 | 36 | ```java 37 | setContentView(R.layout.main_activity); 38 | ... 39 | StatusBarUtil.setColor(MainActivity.this, mColor); 40 | ``` 41 | 42 | 3. If you use this util in a page which containing a `DrawerLayout`, you need add `android:fitsSystemWindows="true"` for `DrawerLayout` in your layout XML: 43 | 44 | ```xml 45 | 52 | 53 | ... 54 | 55 | 56 | ``` 57 | 58 | 4. Set color for swipe back page 59 | 60 | Recommend using with [bingoogolapple/BGASwipeBackLayout\-Android: Android Activity 滑动返回](https://github.com/bingoogolapple/BGASwipeBackLayout-Android) 61 | 62 | ```java 63 | StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha) 64 | ``` 65 | 66 | 5. All `statusBarAlpha` value you set should between 0 ~ 255 67 | 68 | 6. How to use in Fragment, please read [UseInFragmentActivity.java](https://github.com/laobie/StatusBarUtil/blob/master/sample/src/main/java/com/jaeger/statusbarutil/UseInFragmentActivity.java) 69 | 70 | Handle all Fragments in ViewPager as ImageViewFragment, add a fake View in your Fragment layout as StatusBar : 71 | 72 | ```xml 73 | 74 | 78 | 79 | 84 | 85 | 94 | 95 | ``` 96 | 97 | The fake StatusBar View height value ` statusbar_view_height` defined in `dimens.xml` 98 | 99 | ```xml 100 | ~ values-v19/dimens.xml 101 | 102 | 25dp 103 | 104 | ~ values/dimens.xml 105 | 106 | 0dp 107 | ``` 108 | 109 | When you change StatusBarColor : 110 | 111 | ```java 112 | mFakeStatusBar.setBackgroundColor(color); 113 | ``` 114 | 115 | Then in the Activity which contains ViewPage, just invoke 116 | 117 | ```java 118 | StatusBarUtil.setTranslucentForImageViewInFragment(UseInFragmentActivity.this, null); 119 | ``` 120 | 121 | Please read [UseInFragmentActivity.java](https://github.com/laobie/StatusBarUtil/blob/master/sample/src/main/java/com/jaeger/statusbarutil/UseInFragmentActivity.java) 122 | 123 | ### Features 124 | 125 | - Set status bar color 126 | 127 | ```java 128 | StatusBarUtil.setColor(Activity activity, int color) 129 | ``` 130 | 131 | ![](img/set_color.png) 132 | 133 | - Set status bar translucent 134 | 135 | ```java 136 | StatusBarUtil.setTranslucent(Activity activity, int statusBarAlpha) 137 | ``` 138 | 139 | ![](img/set_translucnet.png) 140 | 141 | - Set status bar transparent 142 | 143 | ```java 144 | StatusBarUtil.setTransparent(Activity activity) 145 | ``` 146 | 147 | ![](img/set_transparent.png) 148 | 149 | - Set status bar color for `DrawerLayout` 150 | 151 | ```java 152 | StatusBarUtil.setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, int color) 153 | ``` 154 | 155 | ![](img/set_color_for_drawer_layout.png) 156 | 157 | - Set translucent status bar for using ImageView as head view page 158 | 159 | ```java 160 | StatusBarUtil.setTranslucentForImageView(Activity activity, int statusBarAlpha, View needOffsetView) 161 | ``` 162 | ![](img/set_for_image_view_page.png) 163 | 164 | 165 | - Set Light or Dark mode 166 | 167 | ``` 168 | StatusBarUtil.setLightMode(Activity activity) 169 | StatusBarUtil.setDarkMode(Activity activity) 170 | ``` 171 | ![](img/switch_light_mode.jpeg) 172 | 173 | - Use in fragment 174 | 175 | ![](img/use_in_fragment.gif) 176 | 177 | - Set color for swipe back page 178 | 179 | ```java 180 | StatusBarUtil.setColorForSwipeBack(Activity activity, @ColorInt int color, int statusBarAlpha) 181 | ``` 182 | 183 | ![](img/set_color_for_swipe_back_page.png) 184 | 185 | - Pass statusBarAlpha param when necessary to change your status bar alpha, which is 112 by default. 186 | 187 | 188 | ### License 189 | 190 | Copyright 2016 Jaeger Chen 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.1' 9 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6' 10 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' 11 | } 12 | } 13 | 14 | //apply plugin: 'com.jfrog.bintray' 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | maven { 20 | url "http://dl.bintray.com/laobie/maven" 21 | } 22 | google() 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Sun Feb 14 13:26:50 CST 2016 16 | #systemProp.http.proxyHost=127.0.0.1 17 | #systemProp.http.proxyPort=1080 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Mar 08 15:16:34 CST 2018 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /img/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/3.png -------------------------------------------------------------------------------- /img/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/4.png -------------------------------------------------------------------------------- /img/DRIVE (2).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/DRIVE (2).png -------------------------------------------------------------------------------- /img/Opera Instantánea_2018-05-15_005533_github.com.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/Opera Instantánea_2018-05-15_005533_github.com.png -------------------------------------------------------------------------------- /img/set_color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_color.png -------------------------------------------------------------------------------- /img/set_color_for_drawer_layout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_color_for_drawer_layout.png -------------------------------------------------------------------------------- /img/set_color_for_swipe_back_page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_color_for_swipe_back_page.png -------------------------------------------------------------------------------- /img/set_for_image_view_page.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_for_image_view_page.png -------------------------------------------------------------------------------- /img/set_translucnet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_translucnet.png -------------------------------------------------------------------------------- /img/set_transparent.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/set_transparent.png -------------------------------------------------------------------------------- /img/status_bar_util.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/status_bar_util.png -------------------------------------------------------------------------------- /img/switch_light_mode.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/switch_light_mode.jpeg -------------------------------------------------------------------------------- /img/use_in_fragment.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/img/use_in_fragment.gif -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.github.dcendents.android-maven' 3 | apply plugin: 'com.jfrog.bintray' 4 | 5 | version = "1.5.1" 6 | 7 | android { 8 | compileSdkVersion 27 9 | buildToolsVersion "27.0.3" 10 | 11 | resourcePrefix "statusbarutil_" 12 | 13 | defaultConfig { 14 | minSdkVersion 14 15 | targetSdkVersion 27 16 | versionCode 1 17 | versionName "1.0" 18 | } 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | compile 'com.android.support:appcompat-v7:27.1.0' 30 | compile 'com.android.support:design:27.1.0' 31 | } 32 | 33 | def siteUrl = 'https://github.com/laobie/StatusBarUtil' 34 | def gitUrl = 'https://github.com/laobie/StatusBarUtil.git' 35 | def issueUrl = 'https://github.com/laobie/StatusBarUtil/issues' 36 | group = "com.jaeger.statusbarutil" 37 | install { 38 | repositories.mavenInstaller { 39 | // This generates POM.xml with proper parameters 40 | pom { 41 | project { 42 | packaging 'aar' 43 | name 'Android StatusBar Util' 44 | url siteUrl 45 | licenses { 46 | license { 47 | name 'The Apache Software License, Version 2.0' 48 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 49 | } 50 | } 51 | developers { 52 | developer { 53 | id 'laobie' //填写的一些基本信息 54 | name 'Jaeger Chen' 55 | email 'chjie.jaeger@gmail.com' 56 | } 57 | } 58 | scm { 59 | connection gitUrl 60 | developerConnection gitUrl 61 | url siteUrl 62 | } 63 | } 64 | } 65 | } 66 | } 67 | task sourcesJar(type: Jar) { 68 | from android.sourceSets.main.java.srcDirs 69 | classifier = 'sources' 70 | } 71 | task javadoc(type: Javadoc) { 72 | options.encoding = "UTF-8" 73 | source = android.sourceSets.main.java.srcDirs 74 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 75 | } 76 | task javadocJar(type: Jar, dependsOn: javadoc) { 77 | classifier = 'javadoc' 78 | from javadoc.destinationDir 79 | } 80 | artifacts { 81 | archives javadocJar 82 | archives sourcesJar 83 | } 84 | 85 | Properties properties = new Properties() 86 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 87 | bintray { 88 | user = properties.getProperty("bintray.user") 89 | key = properties.getProperty("bintray.apikey") 90 | configurations = ['archives'] 91 | pkg { 92 | repo = "maven" 93 | name = "StatusBarUtil" 94 | desc = 'Android StatusBar Util' 95 | websiteUrl = siteUrl 96 | issueTrackerUrl = issueUrl 97 | vcsUrl = gitUrl 98 | licenses = ["Apache-2.0"] 99 | labels = ['android'] 100 | publish = true 101 | publicDownloadNumbers = true 102 | } 103 | } -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Jaeger/Develop/android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/androidTest/java/com/example/library/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.library; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /library/src/main/java/com/jaeger/library/StatusBarUtil.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.library; 2 | 3 | import android.annotation.TargetApi; 4 | import android.app.Activity; 5 | import android.content.Context; 6 | import android.graphics.Color; 7 | import android.os.Build; 8 | import android.support.annotation.ColorInt; 9 | import android.support.annotation.IntRange; 10 | import android.support.annotation.NonNull; 11 | import android.support.design.widget.CoordinatorLayout; 12 | import android.support.v4.widget.DrawerLayout; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.view.Window; 16 | import android.view.WindowManager; 17 | import android.widget.LinearLayout; 18 | import java.lang.reflect.Field; 19 | import java.lang.reflect.Method; 20 | 21 | /** 22 | * Created by Jaeger on 16/2/14. 23 | *

24 | * Email: chjie.jaeger@gmail.com 25 | * GitHub: https://github.com/laobie 26 | */ 27 | public class StatusBarUtil { 28 | 29 | public static final int DEFAULT_STATUS_BAR_ALPHA = 112; 30 | private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view; 31 | private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view; 32 | private static final int TAG_KEY_HAVE_SET_OFFSET = -123; 33 | 34 | /** 35 | * 设置状态栏颜色 36 | * 37 | * @param activity 需要设置的 activity 38 | * @param color 状态栏颜色值 39 | */ 40 | public static void setColor(Activity activity, @ColorInt int color) { 41 | setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA); 42 | } 43 | 44 | /** 45 | * 设置状态栏颜色 46 | * 47 | * @param activity 需要设置的activity 48 | * @param color 状态栏颜色值 49 | * @param statusBarAlpha 状态栏透明度 50 | */ 51 | 52 | public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { 53 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 54 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 55 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 56 | activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); 57 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 58 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 59 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 60 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 61 | if (fakeStatusBarView != null) { 62 | if (fakeStatusBarView.getVisibility() == View.GONE) { 63 | fakeStatusBarView.setVisibility(View.VISIBLE); 64 | } 65 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 66 | } else { 67 | decorView.addView(createStatusBarView(activity, color, statusBarAlpha)); 68 | } 69 | setRootView(activity); 70 | } 71 | } 72 | 73 | /** 74 | * 为滑动返回界面设置状态栏颜色 75 | * 76 | * @param activity 需要设置的activity 77 | * @param color 状态栏颜色值 78 | */ 79 | public static void setColorForSwipeBack(Activity activity, int color) { 80 | setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA); 81 | } 82 | 83 | /** 84 | * 为滑动返回界面设置状态栏颜色 85 | * 86 | * @param activity 需要设置的activity 87 | * @param color 状态栏颜色值 88 | * @param statusBarAlpha 状态栏透明度 89 | */ 90 | public static void setColorForSwipeBack(Activity activity, @ColorInt int color, 91 | @IntRange(from = 0, to = 255) int statusBarAlpha) { 92 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 93 | 94 | ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content)); 95 | View rootView = contentView.getChildAt(0); 96 | int statusBarHeight = getStatusBarHeight(activity); 97 | if (rootView != null && rootView instanceof CoordinatorLayout) { 98 | final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView; 99 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 100 | coordinatorLayout.setFitsSystemWindows(false); 101 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 102 | boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight; 103 | if (isNeedRequestLayout) { 104 | contentView.setPadding(0, statusBarHeight, 0, 0); 105 | coordinatorLayout.post(new Runnable() { 106 | @Override 107 | public void run() { 108 | coordinatorLayout.requestLayout(); 109 | } 110 | }); 111 | } 112 | } else { 113 | coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 114 | } 115 | } else { 116 | contentView.setPadding(0, statusBarHeight, 0, 0); 117 | contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); 118 | } 119 | setTransparentForWindow(activity); 120 | } 121 | } 122 | 123 | /** 124 | * 设置状态栏纯色 不加半透明效果 125 | * 126 | * @param activity 需要设置的 activity 127 | * @param color 状态栏颜色值 128 | */ 129 | public static void setColorNoTranslucent(Activity activity, @ColorInt int color) { 130 | setColor(activity, color, 0); 131 | } 132 | 133 | /** 134 | * 设置状态栏颜色(5.0以下无半透明效果,不建议使用) 135 | * 136 | * @param activity 需要设置的 activity 137 | * @param color 状态栏颜色值 138 | */ 139 | @Deprecated 140 | public static void setColorDiff(Activity activity, @ColorInt int color) { 141 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 142 | return; 143 | } 144 | transparentStatusBar(activity); 145 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); 146 | // 移除半透明矩形,以免叠加 147 | View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 148 | if (fakeStatusBarView != null) { 149 | if (fakeStatusBarView.getVisibility() == View.GONE) { 150 | fakeStatusBarView.setVisibility(View.VISIBLE); 151 | } 152 | fakeStatusBarView.setBackgroundColor(color); 153 | } else { 154 | contentView.addView(createStatusBarView(activity, color)); 155 | } 156 | setRootView(activity); 157 | } 158 | 159 | /** 160 | * 使状态栏半透明 161 | *

162 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 163 | * 164 | * @param activity 需要设置的activity 165 | */ 166 | public static void setTranslucent(Activity activity) { 167 | setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA); 168 | } 169 | 170 | /** 171 | * 使状态栏半透明 172 | *

173 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 174 | * 175 | * @param activity 需要设置的activity 176 | * @param statusBarAlpha 状态栏透明度 177 | */ 178 | public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { 179 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 180 | return; 181 | } 182 | setTransparent(activity); 183 | addTranslucentView(activity, statusBarAlpha); 184 | } 185 | 186 | /** 187 | * 针对根布局是 CoordinatorLayout, 使状态栏半透明 188 | *

189 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 190 | * 191 | * @param activity 需要设置的activity 192 | * @param statusBarAlpha 状态栏透明度 193 | */ 194 | public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { 195 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 196 | return; 197 | } 198 | transparentStatusBar(activity); 199 | addTranslucentView(activity, statusBarAlpha); 200 | } 201 | 202 | /** 203 | * 设置状态栏全透明 204 | * 205 | * @param activity 需要设置的activity 206 | */ 207 | public static void setTransparent(Activity activity) { 208 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 209 | return; 210 | } 211 | transparentStatusBar(activity); 212 | setRootView(activity); 213 | } 214 | 215 | /** 216 | * 使状态栏透明(5.0以上半透明效果,不建议使用) 217 | *

218 | * 适用于图片作为背景的界面,此时需要图片填充到状态栏 219 | * 220 | * @param activity 需要设置的activity 221 | */ 222 | @Deprecated 223 | public static void setTranslucentDiff(Activity activity) { 224 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 225 | // 设置状态栏透明 226 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 227 | setRootView(activity); 228 | } 229 | } 230 | 231 | /** 232 | * 为DrawerLayout 布局设置状态栏变色 233 | * 234 | * @param activity 需要设置的activity 235 | * @param drawerLayout DrawerLayout 236 | * @param color 状态栏颜色值 237 | */ 238 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { 239 | setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA); 240 | } 241 | 242 | /** 243 | * 为DrawerLayout 布局设置状态栏颜色,纯色 244 | * 245 | * @param activity 需要设置的activity 246 | * @param drawerLayout DrawerLayout 247 | * @param color 状态栏颜色值 248 | */ 249 | public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { 250 | setColorForDrawerLayout(activity, drawerLayout, color, 0); 251 | } 252 | 253 | /** 254 | * 为DrawerLayout 布局设置状态栏变色 255 | * 256 | * @param activity 需要设置的activity 257 | * @param drawerLayout DrawerLayout 258 | * @param color 状态栏颜色值 259 | * @param statusBarAlpha 状态栏透明度 260 | */ 261 | public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color, 262 | @IntRange(from = 0, to = 255) int statusBarAlpha) { 263 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 264 | return; 265 | } 266 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 267 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 268 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 269 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT); 270 | } else { 271 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 272 | } 273 | // 生成一个状态栏大小的矩形 274 | // 添加 statusBarView 到布局中 275 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); 276 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); 277 | if (fakeStatusBarView != null) { 278 | if (fakeStatusBarView.getVisibility() == View.GONE) { 279 | fakeStatusBarView.setVisibility(View.VISIBLE); 280 | } 281 | fakeStatusBarView.setBackgroundColor(color); 282 | } else { 283 | contentLayout.addView(createStatusBarView(activity, color), 0); 284 | } 285 | // 内容布局不是 LinearLayout 时,设置padding top 286 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { 287 | contentLayout.getChildAt(1) 288 | .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(), 289 | contentLayout.getPaddingRight(), contentLayout.getPaddingBottom()); 290 | } 291 | // 设置属性 292 | setDrawerLayoutProperty(drawerLayout, contentLayout); 293 | addTranslucentView(activity, statusBarAlpha); 294 | } 295 | 296 | /** 297 | * 设置 DrawerLayout 属性 298 | * 299 | * @param drawerLayout DrawerLayout 300 | * @param drawerLayoutContentLayout DrawerLayout 的内容布局 301 | */ 302 | private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) { 303 | ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1); 304 | drawerLayout.setFitsSystemWindows(false); 305 | drawerLayoutContentLayout.setFitsSystemWindows(false); 306 | drawerLayoutContentLayout.setClipToPadding(true); 307 | drawer.setFitsSystemWindows(false); 308 | } 309 | 310 | /** 311 | * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用) 312 | * 313 | * @param activity 需要设置的activity 314 | * @param drawerLayout DrawerLayout 315 | * @param color 状态栏颜色值 316 | */ 317 | @Deprecated 318 | public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { 319 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 320 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 321 | // 生成一个状态栏大小的矩形 322 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); 323 | View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); 324 | if (fakeStatusBarView != null) { 325 | if (fakeStatusBarView.getVisibility() == View.GONE) { 326 | fakeStatusBarView.setVisibility(View.VISIBLE); 327 | } 328 | fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA)); 329 | } else { 330 | // 添加 statusBarView 到布局中 331 | contentLayout.addView(createStatusBarView(activity, color), 0); 332 | } 333 | // 内容布局不是 LinearLayout 时,设置padding top 334 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { 335 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); 336 | } 337 | // 设置属性 338 | setDrawerLayoutProperty(drawerLayout, contentLayout); 339 | } 340 | } 341 | 342 | /** 343 | * 为 DrawerLayout 布局设置状态栏透明 344 | * 345 | * @param activity 需要设置的activity 346 | * @param drawerLayout DrawerLayout 347 | */ 348 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { 349 | setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA); 350 | } 351 | 352 | /** 353 | * 为 DrawerLayout 布局设置状态栏透明 354 | * 355 | * @param activity 需要设置的activity 356 | * @param drawerLayout DrawerLayout 357 | */ 358 | public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, 359 | @IntRange(from = 0, to = 255) int statusBarAlpha) { 360 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 361 | return; 362 | } 363 | setTransparentForDrawerLayout(activity, drawerLayout); 364 | addTranslucentView(activity, statusBarAlpha); 365 | } 366 | 367 | /** 368 | * 为 DrawerLayout 布局设置状态栏透明 369 | * 370 | * @param activity 需要设置的activity 371 | * @param drawerLayout DrawerLayout 372 | */ 373 | public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { 374 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 375 | return; 376 | } 377 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 378 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 379 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 380 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT); 381 | } else { 382 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 383 | } 384 | 385 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); 386 | // 内容布局不是 LinearLayout 时,设置padding top 387 | if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { 388 | contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); 389 | } 390 | 391 | // 设置属性 392 | setDrawerLayoutProperty(drawerLayout, contentLayout); 393 | } 394 | 395 | /** 396 | * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用) 397 | * 398 | * @param activity 需要设置的activity 399 | * @param drawerLayout DrawerLayout 400 | */ 401 | @Deprecated 402 | public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) { 403 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 404 | // 设置状态栏透明 405 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 406 | // 设置内容布局属性 407 | ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); 408 | contentLayout.setFitsSystemWindows(true); 409 | contentLayout.setClipToPadding(true); 410 | // 设置抽屉布局属性 411 | ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1); 412 | vg.setFitsSystemWindows(false); 413 | // 设置 DrawerLayout 属性 414 | drawerLayout.setFitsSystemWindows(false); 415 | } 416 | } 417 | 418 | /** 419 | * 为头部是 ImageView 的界面设置状态栏全透明 420 | * 421 | * @param activity 需要设置的activity 422 | * @param needOffsetView 需要向下偏移的 View 423 | */ 424 | public static void setTransparentForImageView(Activity activity, View needOffsetView) { 425 | setTranslucentForImageView(activity, 0, needOffsetView); 426 | } 427 | 428 | /** 429 | * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度) 430 | * 431 | * @param activity 需要设置的activity 432 | * @param needOffsetView 需要向下偏移的 View 433 | */ 434 | public static void setTranslucentForImageView(Activity activity, View needOffsetView) { 435 | setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); 436 | } 437 | 438 | /** 439 | * 为头部是 ImageView 的界面设置状态栏透明 440 | * 441 | * @param activity 需要设置的activity 442 | * @param statusBarAlpha 状态栏透明度 443 | * @param needOffsetView 需要向下偏移的 View 444 | */ 445 | public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, 446 | View needOffsetView) { 447 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 448 | return; 449 | } 450 | setTransparentForWindow(activity); 451 | addTranslucentView(activity, statusBarAlpha); 452 | if (needOffsetView != null) { 453 | Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET); 454 | if (haveSetOffset != null && (Boolean) haveSetOffset) { 455 | return; 456 | } 457 | ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams(); 458 | layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity), 459 | layoutParams.rightMargin, layoutParams.bottomMargin); 460 | needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true); 461 | } 462 | } 463 | 464 | /** 465 | * 为 fragment 头部是 ImageView 的设置状态栏透明 466 | * 467 | * @param activity fragment 对应的 activity 468 | * @param needOffsetView 需要向下偏移的 View 469 | */ 470 | public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) { 471 | setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); 472 | } 473 | 474 | /** 475 | * 为 fragment 头部是 ImageView 的设置状态栏透明 476 | * 477 | * @param activity fragment 对应的 activity 478 | * @param needOffsetView 需要向下偏移的 View 479 | */ 480 | public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) { 481 | setTranslucentForImageViewInFragment(activity, 0, needOffsetView); 482 | } 483 | 484 | /** 485 | * 为 fragment 头部是 ImageView 的设置状态栏透明 486 | * 487 | * @param activity fragment 对应的 activity 488 | * @param statusBarAlpha 状态栏透明度 489 | * @param needOffsetView 需要向下偏移的 View 490 | */ 491 | public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, 492 | View needOffsetView) { 493 | setTranslucentForImageView(activity, statusBarAlpha, needOffsetView); 494 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 495 | clearPreviousSetting(activity); 496 | } 497 | } 498 | 499 | /** 500 | * 隐藏伪状态栏 View 501 | * 502 | * @param activity 调用的 Activity 503 | */ 504 | public static void hideFakeStatusBarView(Activity activity) { 505 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 506 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 507 | if (fakeStatusBarView != null) { 508 | fakeStatusBarView.setVisibility(View.GONE); 509 | } 510 | View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); 511 | if (fakeTranslucentView != null) { 512 | fakeTranslucentView.setVisibility(View.GONE); 513 | } 514 | } 515 | 516 | @TargetApi(Build.VERSION_CODES.M) 517 | public static void setLightMode(Activity activity) { 518 | setMIUIStatusBarDarkIcon(activity, true); 519 | setMeizuStatusBarDarkIcon(activity, true); 520 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 521 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 522 | } 523 | } 524 | 525 | @TargetApi(Build.VERSION_CODES.M) 526 | public static void setDarkMode(Activity activity) { 527 | setMIUIStatusBarDarkIcon(activity, false); 528 | setMeizuStatusBarDarkIcon(activity, false); 529 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 530 | activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 531 | } 532 | } 533 | 534 | /** 535 | * 修改 MIUI V6 以上状态栏颜色 536 | */ 537 | private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { 538 | Class clazz = activity.getWindow().getClass(); 539 | try { 540 | Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); 541 | Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); 542 | int darkModeFlag = field.getInt(layoutParams); 543 | Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); 544 | extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag); 545 | } catch (Exception e) { 546 | //e.printStackTrace(); 547 | } 548 | } 549 | 550 | /** 551 | * 修改魅族状态栏字体颜色 Flyme 4.0 552 | */ 553 | private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { 554 | try { 555 | WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); 556 | Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); 557 | Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); 558 | darkFlag.setAccessible(true); 559 | meizuFlags.setAccessible(true); 560 | int bit = darkFlag.getInt(null); 561 | int value = meizuFlags.getInt(lp); 562 | if (darkIcon) { 563 | value |= bit; 564 | } else { 565 | value &= ~bit; 566 | } 567 | meizuFlags.setInt(lp, value); 568 | activity.getWindow().setAttributes(lp); 569 | } catch (Exception e) { 570 | //e.printStackTrace(); 571 | } 572 | } 573 | 574 | /////////////////////////////////////////////////////////////////////////////////// 575 | 576 | @TargetApi(Build.VERSION_CODES.KITKAT) 577 | private static void clearPreviousSetting(Activity activity) { 578 | ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 579 | View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); 580 | if (fakeStatusBarView != null) { 581 | decorView.removeView(fakeStatusBarView); 582 | ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0); 583 | rootView.setPadding(0, 0, 0, 0); 584 | } 585 | } 586 | 587 | /** 588 | * 添加半透明矩形条 589 | * 590 | * @param activity 需要设置的 activity 591 | * @param statusBarAlpha 透明值 592 | */ 593 | private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { 594 | ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); 595 | View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); 596 | if (fakeTranslucentView != null) { 597 | if (fakeTranslucentView.getVisibility() == View.GONE) { 598 | fakeTranslucentView.setVisibility(View.VISIBLE); 599 | } 600 | fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0)); 601 | } else { 602 | contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha)); 603 | } 604 | } 605 | 606 | /** 607 | * 生成一个和状态栏大小相同的彩色矩形条 608 | * 609 | * @param activity 需要设置的 activity 610 | * @param color 状态栏颜色值 611 | * @return 状态栏矩形条 612 | */ 613 | private static View createStatusBarView(Activity activity, @ColorInt int color) { 614 | return createStatusBarView(activity, color, 0); 615 | } 616 | 617 | /** 618 | * 生成一个和状态栏大小相同的半透明矩形条 619 | * 620 | * @param activity 需要设置的activity 621 | * @param color 状态栏颜色值 622 | * @param alpha 透明值 623 | * @return 状态栏矩形条 624 | */ 625 | private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) { 626 | // 绘制一个和状态栏一样高的矩形 627 | View statusBarView = new View(activity); 628 | LinearLayout.LayoutParams params = 629 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); 630 | statusBarView.setLayoutParams(params); 631 | statusBarView.setBackgroundColor(calculateStatusColor(color, alpha)); 632 | statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID); 633 | return statusBarView; 634 | } 635 | 636 | /** 637 | * 设置根布局参数 638 | */ 639 | private static void setRootView(Activity activity) { 640 | ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content); 641 | for (int i = 0, count = parent.getChildCount(); i < count; i++) { 642 | View childView = parent.getChildAt(i); 643 | if (childView instanceof ViewGroup) { 644 | childView.setFitsSystemWindows(true); 645 | ((ViewGroup) childView).setClipToPadding(true); 646 | } 647 | } 648 | } 649 | 650 | /** 651 | * 设置透明 652 | */ 653 | private static void setTransparentForWindow(Activity activity) { 654 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 655 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT); 656 | activity.getWindow() 657 | .getDecorView() 658 | .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 659 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 660 | activity.getWindow() 661 | .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 662 | } 663 | } 664 | 665 | /** 666 | * 使状态栏透明 667 | */ 668 | @TargetApi(Build.VERSION_CODES.KITKAT) 669 | private static void transparentStatusBar(Activity activity) { 670 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 671 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 672 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 673 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 674 | activity.getWindow().setStatusBarColor(Color.TRANSPARENT); 675 | } else { 676 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 677 | } 678 | } 679 | 680 | /** 681 | * 创建半透明矩形 View 682 | * 683 | * @param alpha 透明值 684 | * @return 半透明 View 685 | */ 686 | private static View createTranslucentStatusBarView(Activity activity, int alpha) { 687 | // 绘制一个和状态栏一样高的矩形 688 | View statusBarView = new View(activity); 689 | LinearLayout.LayoutParams params = 690 | new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); 691 | statusBarView.setLayoutParams(params); 692 | statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0)); 693 | statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID); 694 | return statusBarView; 695 | } 696 | 697 | /** 698 | * 获取状态栏高度 699 | * 700 | * @param context context 701 | * @return 状态栏高度 702 | */ 703 | private static int getStatusBarHeight(Context context) { 704 | // 获得状态栏高度 705 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); 706 | return context.getResources().getDimensionPixelSize(resourceId); 707 | } 708 | 709 | /** 710 | * 计算状态栏颜色 711 | * 712 | * @param color color值 713 | * @param alpha alpha值 714 | * @return 最终的状态栏颜色 715 | */ 716 | private static int calculateStatusColor(@ColorInt int color, int alpha) { 717 | if (alpha == 0) { 718 | return color; 719 | } 720 | float a = 1 - alpha / 255f; 721 | int red = color >> 16 & 0xff; 722 | int green = color >> 8 & 0xff; 723 | int blue = color & 0xff; 724 | red = (int) (red * a + 0.5); 725 | green = (int) (green * a + 0.5); 726 | blue = (int) (blue * a + 0.5); 727 | return 0xff << 24 | red << 16 | green << 8 | blue; 728 | } 729 | } 730 | -------------------------------------------------------------------------------- /library/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /library/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /sample/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /sample/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | 5 | compileSdkVersion 27 6 | buildToolsVersion "27.0.3" 7 | 8 | defaultConfig { 9 | applicationId "com.jaeger.statusbardemo" 10 | minSdkVersion 14 11 | targetSdkVersion 27 12 | versionCode 8 13 | versionName "1.5.0" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(include: ['*.jar'], dir: 'libs') 25 | testCompile 'junit:junit:4.12' 26 | compile 'com.android.support:appcompat-v7:27.1.0' 27 | compile 'com.android.support:design:27.1.0' 28 | compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' 29 | compile 'com.r0adkll:slidableactivity:2.0.5' 30 | compile project(path: ':library') 31 | // compile 'com.jaeger.statusbaruitl:library:1.3.2' 32 | } 33 | -------------------------------------------------------------------------------- /sample/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/Jaeger/Develop/android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /sample/release/output.json: -------------------------------------------------------------------------------- 1 | [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":6},"path":"sample-release.apk","properties":{"packageId":"com.jaeger.statusbardemo","split":"","minSdkVersion":"14"}}] -------------------------------------------------------------------------------- /sample/release/sample-release.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/release/sample-release.apk -------------------------------------------------------------------------------- /sample/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.view.MenuItem; 5 | import com.jaeger.library.StatusBarUtil; 6 | import com.jaeger.statusbardemo.R; 7 | 8 | /** 9 | * Created by Jaeger on 16/2/14. 10 | * 11 | * Email: chjie.jaeger@gmail.com 12 | * GitHub: https://github.com/laobie 13 | */ 14 | public class BaseActivity extends AppCompatActivity { 15 | 16 | @Override 17 | public void setContentView(int layoutResID) { 18 | super.setContentView(layoutResID); 19 | setStatusBar(); 20 | } 21 | 22 | protected void setStatusBar() { 23 | StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary)); 24 | } 25 | 26 | @Override 27 | public boolean onOptionsItemSelected(MenuItem item) { 28 | if (item.getItemId() == android.R.id.home) { 29 | finish(); 30 | } 31 | return super.onOptionsItemSelected(item); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/ColorStatusBarActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.widget.Toolbar; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.SeekBar; 8 | import android.widget.TextView; 9 | import com.jaeger.library.StatusBarUtil; 10 | import com.jaeger.statusbardemo.R; 11 | import java.util.Random; 12 | 13 | /** 14 | * Created by Jaeger on 16/2/14. 15 | * 16 | * Email: chjie.jaeger@gmail.com 17 | * GitHub: https://github.com/laobie 18 | */ 19 | public class ColorStatusBarActivity extends BaseActivity { 20 | private Toolbar mToolbar; 21 | private Button mBtnChangeColor; 22 | private SeekBar mSbChangeAlpha; 23 | private TextView mTvStatusAlpha; 24 | 25 | private int mColor; 26 | private int mAlpha; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_color_status_bar); 32 | 33 | mToolbar = findViewById(R.id.toolbar); 34 | mBtnChangeColor = findViewById(R.id.btn_change_color); 35 | mTvStatusAlpha = findViewById(R.id.tv_status_alpha); 36 | mSbChangeAlpha = findViewById(R.id.sb_change_alpha); 37 | 38 | // 设置toolbar 39 | setSupportActionBar(mToolbar); 40 | if (getSupportActionBar() != null) { 41 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 42 | } 43 | 44 | // 改变颜色 45 | mBtnChangeColor.setOnClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View v) { 48 | Random random = new Random(); 49 | mColor = 0xff000000 | random.nextInt(0xffffff); 50 | mToolbar.setBackgroundColor(mColor); 51 | StatusBarUtil.setColor(ColorStatusBarActivity.this, mColor, mAlpha); 52 | } 53 | }); 54 | 55 | mSbChangeAlpha.setMax(255); 56 | mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 57 | @Override 58 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 59 | mAlpha = progress; 60 | StatusBarUtil.setColor(ColorStatusBarActivity.this, mColor, mAlpha); 61 | mTvStatusAlpha.setText(String.valueOf(mAlpha)); 62 | } 63 | 64 | @Override 65 | public void onStartTrackingTouch(SeekBar seekBar) { 66 | 67 | } 68 | 69 | @Override 70 | public void onStopTrackingTouch(SeekBar seekBar) { 71 | 72 | } 73 | }); 74 | mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); 75 | } 76 | 77 | @Override 78 | protected void setStatusBar() { 79 | mColor = getResources().getColor(R.color.colorPrimary); 80 | StatusBarUtil.setColor(this, mColor); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/ImageFragment.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import com.jaeger.statusbardemo.R; 10 | 11 | /** 12 | * Created by Jaeger on 16/8/11. 13 | * 14 | * Email: chjie.jaeger@gmail.com 15 | * GitHub: https://github.com/laobie 16 | */ 17 | public class ImageFragment extends Fragment { 18 | 19 | @Nullable 20 | @Override 21 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 22 | return inflater.inflate(R.layout.fragement_image, container, false); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/ImageStatusBarActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.Button; 6 | import android.widget.RelativeLayout; 7 | import android.widget.SeekBar; 8 | import android.widget.TextView; 9 | import com.jaeger.library.StatusBarUtil; 10 | import com.jaeger.statusbardemo.R; 11 | 12 | /** 13 | * Created by Jaeger on 16/2/14. 14 | * 15 | * Email: chjie.jaeger@gmail.com 16 | * GitHub: https://github.com/laobie 17 | */ 18 | public class ImageStatusBarActivity extends BaseActivity { 19 | public static final String EXTRA_IS_TRANSPARENT = "is_transparent"; 20 | private TextView mTvStatusAlpha; 21 | private RelativeLayout mRootLayout; 22 | private Button mBtnChangeBackground; 23 | private boolean isBgChanged; 24 | private SeekBar mSbChangeAlpha; 25 | 26 | private boolean isTransparent; 27 | private int mAlpha; 28 | 29 | @Override 30 | protected void onCreate(Bundle savedInstanceState) { 31 | isTransparent = getIntent().getBooleanExtra(EXTRA_IS_TRANSPARENT, false); 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.activity_image_status_bar); 34 | 35 | mRootLayout = (RelativeLayout) findViewById(R.id.root_layout); 36 | mBtnChangeBackground = (Button) findViewById(R.id.btn_change_background); 37 | mTvStatusAlpha = (TextView) findViewById(R.id.tv_status_alpha); 38 | mSbChangeAlpha = (SeekBar) findViewById(R.id.sb_change_alpha); 39 | 40 | mBtnChangeBackground.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | isBgChanged = !isBgChanged; 44 | if (isBgChanged) { 45 | mRootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_girl)); 46 | } else { 47 | mRootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_monkey)); 48 | } 49 | } 50 | }); 51 | 52 | if (!isTransparent) { 53 | mSbChangeAlpha.setVisibility(View.VISIBLE); 54 | setSeekBar(); 55 | } else { 56 | mSbChangeAlpha.setVisibility(View.GONE); 57 | } 58 | } 59 | 60 | @Override 61 | protected void setStatusBar() { 62 | if (isTransparent) { 63 | StatusBarUtil.setTransparent(this); 64 | } else { 65 | StatusBarUtil.setTranslucent(this, StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); 66 | } 67 | } 68 | 69 | private void setSeekBar() { 70 | mSbChangeAlpha.setMax(255); 71 | mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 72 | @Override 73 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 74 | mAlpha = progress; 75 | StatusBarUtil.setTranslucent(ImageStatusBarActivity.this, mAlpha); 76 | mTvStatusAlpha.setText(String.valueOf(mAlpha)); 77 | } 78 | 79 | @Override 80 | public void onStartTrackingTouch(SeekBar seekBar) { 81 | 82 | } 83 | 84 | @Override 85 | public void onStopTrackingTouch(SeekBar seekBar) { 86 | 87 | } 88 | }); 89 | mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/ImageViewActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.widget.Toolbar; 6 | import android.view.MenuItem; 7 | import android.view.View; 8 | import android.widget.SeekBar; 9 | import android.widget.TextView; 10 | import com.jaeger.library.StatusBarUtil; 11 | import com.jaeger.statusbardemo.R; 12 | import com.r0adkll.slidr.Slidr; 13 | 14 | /** 15 | * Created by Jaeger on 16/7/12. 16 | * 17 | * Email: chjie.jaeger@gmail.com 18 | * GitHub: https://github.com/laobie 19 | */ 20 | public class ImageViewActivity extends BaseActivity { 21 | private Toolbar mToolbar; 22 | private View mViewNeedOffset; 23 | private SeekBar mSbChangeAlpha; 24 | private TextView mTvStatusAlpha; 25 | 26 | private int mAlpha; 27 | 28 | @Override 29 | protected void onCreate(@Nullable Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_image_view); 32 | // 设置右滑动返回 33 | Slidr.attach(this); 34 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 35 | mViewNeedOffset = findViewById(R.id.view_need_offset); 36 | mTvStatusAlpha = (TextView) findViewById(R.id.tv_status_alpha); 37 | mSbChangeAlpha = (SeekBar) findViewById(R.id.sb_change_alpha); 38 | 39 | setSupportActionBar(mToolbar); 40 | if (getSupportActionBar() != null) { 41 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 42 | } 43 | 44 | mSbChangeAlpha.setMax(255); 45 | mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 46 | @Override 47 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 48 | mAlpha = progress; 49 | StatusBarUtil.setTranslucentForImageView(ImageViewActivity.this, mAlpha, mViewNeedOffset); 50 | mTvStatusAlpha.setText(String.valueOf(mAlpha)); 51 | } 52 | 53 | @Override 54 | public void onStartTrackingTouch(SeekBar seekBar) { 55 | 56 | } 57 | 58 | @Override 59 | public void onStopTrackingTouch(SeekBar seekBar) { 60 | 61 | } 62 | }); 63 | mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); 64 | } 65 | 66 | @Override 67 | protected void setStatusBar() { 68 | mViewNeedOffset = findViewById(R.id.view_need_offset); 69 | StatusBarUtil.setTranslucentForImageView(this, mViewNeedOffset); 70 | } 71 | 72 | @Override 73 | public boolean onOptionsItemSelected(MenuItem item) { 74 | if (item.getItemId() == android.R.id.home) { 75 | finish(); 76 | } 77 | return super.onOptionsItemSelected(item); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v4.widget.DrawerLayout; 6 | import android.support.v7.app.ActionBarDrawerToggle; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.Button; 11 | import android.widget.CheckBox; 12 | import android.widget.SeekBar; 13 | import android.widget.TextView; 14 | import com.jaeger.library.StatusBarUtil; 15 | import com.jaeger.statusbardemo.R; 16 | 17 | /** 18 | * Created by Jaeger on 16/2/14. 19 | * 20 | * Email: chjie.jaeger@gmail.com 21 | * GitHub: https://github.com/laobie 22 | */ 23 | public class MainActivity extends BaseActivity { 24 | private DrawerLayout mDrawerLayout; 25 | private Toolbar mToolbar; 26 | private CheckBox mChbTranslucent; 27 | private Button mBtnSetColor; 28 | private Button mBtnSetTransparent; 29 | private Button mBtnSetTranslucent; 30 | private Button mBtnSetForImageView; 31 | private Button mBtnUseInFragment; 32 | private Button mBtnSetColorForSwipeBack; 33 | private Button mBtnSwitchMode; 34 | 35 | private ViewGroup contentLayout; 36 | private SeekBar mSbChangeAlpha; 37 | private TextView mTvStatusAlpha; 38 | 39 | private int mStatusBarColor; 40 | private int mAlpha = StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA; 41 | 42 | @Override 43 | protected void onCreate(Bundle savedInstanceState) { 44 | super.onCreate(savedInstanceState); 45 | setContentView(R.layout.activity_main); 46 | 47 | mDrawerLayout = findViewById(R.id.drawer_layout); 48 | contentLayout = findViewById(R.id.main); 49 | mToolbar = findViewById(R.id.toolbar); 50 | mChbTranslucent = findViewById(R.id.chb_translucent); 51 | mBtnSetColor = findViewById(R.id.btn_set_color); 52 | mBtnSetTransparent = findViewById(R.id.btn_set_transparent); 53 | mBtnSetTranslucent = findViewById(R.id.btn_set_translucent); 54 | mBtnSetForImageView = findViewById(R.id.btn_set_for_image_view); 55 | mBtnUseInFragment = findViewById(R.id.btn_use_in_fragment); 56 | mBtnSetColorForSwipeBack = findViewById(R.id.btn_set_color_for_swipe_back); 57 | mBtnSwitchMode = findViewById(R.id.btn_switch_mode); 58 | mSbChangeAlpha = findViewById(R.id.sb_change_alpha); 59 | mTvStatusAlpha = findViewById(R.id.tv_status_alpha); 60 | setSupportActionBar(mToolbar); 61 | 62 | ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, 63 | R.string.navigation_drawer_close); 64 | mDrawerLayout.setDrawerListener(toggle); 65 | toggle.syncState(); 66 | 67 | mBtnSetColor.setOnClickListener(new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | Intent intent = new Intent(MainActivity.this, ColorStatusBarActivity.class); 71 | startActivity(intent); 72 | } 73 | }); 74 | 75 | mBtnSetTransparent.setOnClickListener(new View.OnClickListener() { 76 | @Override 77 | public void onClick(View v) { 78 | Intent intent = new Intent(MainActivity.this, ImageStatusBarActivity.class); 79 | intent.putExtra(ImageStatusBarActivity.EXTRA_IS_TRANSPARENT, true); 80 | startActivity(intent); 81 | } 82 | }); 83 | 84 | mBtnSetTranslucent.setOnClickListener(new View.OnClickListener() { 85 | @Override 86 | public void onClick(View v) { 87 | Intent intent = new Intent(MainActivity.this, ImageStatusBarActivity.class); 88 | intent.putExtra(ImageStatusBarActivity.EXTRA_IS_TRANSPARENT, false); 89 | startActivity(intent); 90 | } 91 | }); 92 | 93 | mBtnSetForImageView.setOnClickListener(new View.OnClickListener() { 94 | @Override 95 | public void onClick(View v) { 96 | Intent intent = new Intent(MainActivity.this, ImageViewActivity.class); 97 | startActivity(intent); 98 | } 99 | }); 100 | 101 | mBtnUseInFragment.setOnClickListener(new View.OnClickListener() { 102 | @Override 103 | public void onClick(View v) { 104 | Intent intent = new Intent(MainActivity.this, UseInFragmentActivity.class); 105 | startActivity(intent); 106 | } 107 | }); 108 | 109 | mBtnSetColorForSwipeBack.setOnClickListener(new View.OnClickListener() { 110 | @Override 111 | public void onClick(View v) { 112 | Intent intent = new Intent(MainActivity.this, SwipeBackActivity.class); 113 | startActivity(intent); 114 | } 115 | }); 116 | 117 | mBtnSwitchMode.setOnClickListener(new View.OnClickListener() { 118 | @Override 119 | public void onClick(View view) { 120 | Intent intent = new Intent(MainActivity.this, SwitchModeActivity.class); 121 | startActivity(intent); 122 | } 123 | }); 124 | 125 | mChbTranslucent.setOnClickListener(new View.OnClickListener() { 126 | @Override 127 | public void onClick(View v) { 128 | if (mChbTranslucent.isChecked()) { 129 | contentLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_monkey)); 130 | StatusBarUtil.setTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, mAlpha); 131 | mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent)); 132 | } else { 133 | contentLayout.setBackgroundDrawable(null); 134 | mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); 135 | StatusBarUtil.setColorForDrawerLayout(MainActivity.this, mDrawerLayout, 136 | getResources().getColor(R.color.colorPrimary), mAlpha); 137 | } 138 | } 139 | }); 140 | 141 | mSbChangeAlpha.setMax(255); 142 | mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { 143 | @Override 144 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 145 | mAlpha = progress; 146 | if (mChbTranslucent.isChecked()) { 147 | StatusBarUtil.setTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, mAlpha); 148 | } else { 149 | StatusBarUtil.setColorForDrawerLayout(MainActivity.this, mDrawerLayout, mStatusBarColor, mAlpha); 150 | } 151 | mTvStatusAlpha.setText(String.valueOf(mAlpha)); 152 | } 153 | 154 | @Override 155 | public void onStartTrackingTouch(SeekBar seekBar) { 156 | 157 | } 158 | 159 | @Override 160 | public void onStopTrackingTouch(SeekBar seekBar) { 161 | 162 | } 163 | }); 164 | mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); 165 | } 166 | 167 | @Override 168 | protected void setStatusBar() { 169 | mStatusBarColor = getResources().getColor(R.color.colorPrimary); 170 | StatusBarUtil.setColorForDrawerLayout(this, (DrawerLayout) findViewById(R.id.drawer_layout), mStatusBarColor, mAlpha); 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/SimpleFragment.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.ColorInt; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | import com.jaeger.statusbardemo.R; 12 | 13 | /** 14 | * Created by Jaeger on 16/8/11. 15 | * 16 | * Email: chjie.jaeger@gmail.com 17 | * GitHub: https://github.com/laobie 18 | */ 19 | public class SimpleFragment extends Fragment { 20 | private TextView mTvTitle; 21 | private View mFakeStatusBar; 22 | 23 | @Override 24 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 25 | return inflater.inflate(R.layout.fragement_simple, container, false); 26 | } 27 | 28 | @Override 29 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 30 | super.onViewCreated(view, savedInstanceState); 31 | mTvTitle = (TextView) view.findViewById(R.id.tv_title); 32 | mFakeStatusBar = view.findViewById(R.id.fake_status_bar); 33 | } 34 | 35 | public void setTvTitleBackgroundColor(@ColorInt int color) { 36 | mTvTitle.setBackgroundColor(color); 37 | mFakeStatusBar.setBackgroundColor(color); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/SwipeBackActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.widget.Toolbar; 7 | import android.view.View; 8 | import android.widget.Button; 9 | import com.jaeger.library.StatusBarUtil; 10 | import com.jaeger.statusbardemo.R; 11 | import com.r0adkll.slidr.Slidr; 12 | import java.util.Random; 13 | 14 | /** 15 | * Created by Jaeger on 2016/10/15. 16 | * 17 | * Email: chjie.jaeger@gmail.com 18 | * GitHub: https://github.com/laobie 19 | */ 20 | 21 | public class SwipeBackActivity extends BaseActivity { 22 | private Button mBtnChangeColor; 23 | private int mColor = Color.GRAY; 24 | private Toolbar mToolbar; 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | // 设置右滑动返回 30 | Slidr.attach(this); 31 | setContentView(R.layout.swipe_back_activity); 32 | mToolbar = (Toolbar) findViewById(R.id.toolbar); 33 | mBtnChangeColor = (Button) findViewById(R.id.btn_change_color); 34 | 35 | setSupportActionBar(mToolbar); 36 | if (getSupportActionBar() != null) { 37 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 38 | } 39 | mToolbar.setBackgroundColor(mColor); 40 | mBtnChangeColor.setOnClickListener(new View.OnClickListener() { 41 | @Override 42 | public void onClick(View v) { 43 | Random random = new Random(); 44 | mColor = 0xff000000 | random.nextInt(0xffffff); 45 | mToolbar.setBackgroundColor(mColor); 46 | StatusBarUtil.setColorForSwipeBack(SwipeBackActivity.this, mColor, 38); 47 | } 48 | }); 49 | } 50 | 51 | @Override 52 | protected void setStatusBar() { 53 | StatusBarUtil.setColorForSwipeBack(this, mColor, 38); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/SwitchModeActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.widget.Toolbar; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import com.jaeger.library.StatusBarUtil; 8 | import com.jaeger.statusbardemo.R; 9 | 10 | /** 11 | * Created by jaeger on 08/03/2018. 12 | * 13 | * Email: chjie.jaeger@gmail.com 14 | * GitHub: https://github.com/laobie 15 | */ 16 | 17 | public class SwitchModeActivity extends BaseActivity { 18 | private Toolbar mToolbar; 19 | private Button mBtnSetLightMode; 20 | private Button mBtnSetDarkMode; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_switch_mode); 26 | 27 | mToolbar = findViewById(R.id.toolbar); 28 | mBtnSetLightMode = findViewById(R.id.btn_set_light_mode); 29 | mBtnSetDarkMode = findViewById(R.id.btn_set_dark_mode); 30 | 31 | // 设置toolbar 32 | setSupportActionBar(mToolbar); 33 | if (getSupportActionBar() != null) { 34 | getSupportActionBar().setDisplayHomeAsUpEnabled(true); 35 | } 36 | 37 | mBtnSetLightMode.setOnClickListener(new View.OnClickListener() { 38 | @Override 39 | public void onClick(View view) { 40 | int color = getResources().getColor(R.color.light_gray); 41 | StatusBarUtil.setColor(SwitchModeActivity.this, color, 30); 42 | mToolbar.setBackgroundColor(color); 43 | StatusBarUtil.setLightMode(SwitchModeActivity.this); 44 | } 45 | }); 46 | 47 | mBtnSetDarkMode.setOnClickListener(new View.OnClickListener() { 48 | @Override 49 | public void onClick(View view) { 50 | int color = getResources().getColor(R.color.colorPrimary); 51 | StatusBarUtil.setColor(SwitchModeActivity.this, color); 52 | mToolbar.setBackgroundColor(color); 53 | StatusBarUtil.setDarkMode(SwitchModeActivity.this); 54 | } 55 | }); 56 | } 57 | 58 | @Override 59 | protected void setStatusBar() { 60 | StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary)); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /sample/src/main/java/com/jaeger/statusbarutil/UseInFragmentActivity.java: -------------------------------------------------------------------------------- 1 | package com.jaeger.statusbarutil; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.FragmentPagerAdapter; 7 | import android.support.v4.view.ViewPager; 8 | import com.ashokvarma.bottomnavigation.BottomNavigationBar; 9 | import com.ashokvarma.bottomnavigation.BottomNavigationItem; 10 | import com.jaeger.library.StatusBarUtil; 11 | import com.jaeger.statusbardemo.R; 12 | import java.util.ArrayList; 13 | import java.util.Random; 14 | 15 | /** 16 | * Created by Jaeger on 16/8/11. 17 | * 18 | * Email: chjie.jaeger@gmail.com 19 | * GitHub: https://github.com/laobie 20 | */ 21 | public class UseInFragmentActivity extends BaseActivity { 22 | private ViewPager mVpHome; 23 | private BottomNavigationBar mBottomNavigationBar; 24 | private ArrayList mFragmentList = new ArrayList<>(); 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_use_in_fragment); 30 | mVpHome = (ViewPager) findViewById(R.id.vp_home); 31 | mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); 32 | mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_favorite, "One")) 33 | .addItem(new BottomNavigationItem(R.drawable.ic_gavel, "Two")) 34 | .addItem(new BottomNavigationItem(R.drawable.ic_grade, "Three")) 35 | .addItem(new BottomNavigationItem(R.drawable.ic_group_work, "Four")) 36 | .initialise(); 37 | 38 | mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() { 39 | @Override 40 | public void onTabSelected(int position) { 41 | mVpHome.setCurrentItem(position); 42 | } 43 | 44 | @Override 45 | public void onTabUnselected(int position) { 46 | 47 | } 48 | 49 | @Override 50 | public void onTabReselected(int position) { 51 | 52 | } 53 | }); 54 | 55 | mFragmentList.add(new ImageFragment()); 56 | mFragmentList.add(new SimpleFragment()); 57 | mFragmentList.add(new SimpleFragment()); 58 | mFragmentList.add(new SimpleFragment()); 59 | 60 | mVpHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 61 | @Override 62 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 63 | 64 | } 65 | 66 | @Override 67 | public void onPageSelected(int position) { 68 | mBottomNavigationBar.selectTab(position); 69 | switch (position) { 70 | case 0: 71 | break; 72 | default: 73 | Random random = new Random(); 74 | int color = 0xff000000 | random.nextInt(0xffffff); 75 | if (mFragmentList.get(position) instanceof SimpleFragment) { 76 | ((SimpleFragment) mFragmentList.get(position)).setTvTitleBackgroundColor(color); 77 | } 78 | break; 79 | } 80 | } 81 | 82 | @Override 83 | public void onPageScrollStateChanged(int state) { 84 | 85 | } 86 | }); 87 | mVpHome.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { 88 | @Override 89 | public Fragment getItem(int position) { 90 | return mFragmentList.get(position); 91 | } 92 | 93 | @Override 94 | public int getCount() { 95 | return mFragmentList.size(); 96 | } 97 | }); 98 | } 99 | 100 | @Override 101 | protected void setStatusBar() { 102 | StatusBarUtil.setTranslucentForImageViewInFragment(UseInFragmentActivity.this, null); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /sample/src/main/res/anim/slide_in_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/anim/slide_in_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/anim/slide_out_left.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | -------------------------------------------------------------------------------- /sample/src/main/res/anim/slide_out_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/bg_girl.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/bg_girl.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/bg_monkey.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/bg_monkey.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/bg_monkey_king.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/bg_monkey_king.jpg -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/ic_avatar.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_favorite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/ic_favorite.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_gavel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/ic_gavel.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_grade.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/ic_grade.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_group_work.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/ic_group_work.png -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_camera.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_gallery.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_manage.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_send.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_share.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/ic_menu_slideshow.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /sample/src/main/res/drawable/nav_header_bg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/laobie/StatusBarUtil/8bc86216b7538e8bf70de721d7415226d47bd5dd/sample/src/main/res/drawable/nav_header_bg.jpg -------------------------------------------------------------------------------- /sample/src/main/res/layout/activity_color_status_bar.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 18 | 19 | 27 | 28 |