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

* Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class StatusBarUtil { public static final int DEFAULT_STATUS_BAR_ALPHA = 112; private static final int FAKE_STATUS_BAR_VIEW_ID = R.id.statusbarutil_fake_status_bar_view; private static final int FAKE_TRANSLUCENT_VIEW_ID = R.id.statusbarutil_translucent_view; private static final int TAG_KEY_HAVE_SET_OFFSET = -123; /** * 设置状态栏颜色 * * @param activity 需要设置的 activity * @param color 状态栏颜色值 */ public static void setColor(Activity activity, @ColorInt int color) { setColor(activity, color, DEFAULT_STATUS_BAR_ALPHA); } /** * 设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } else { decorView.addView(createStatusBarView(activity, color, statusBarAlpha)); } setRootView(activity); } } /** * 为滑动返回界面设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 */ public static void setColorForSwipeBack(Activity activity, int color) { setColorForSwipeBack(activity, color, DEFAULT_STATUS_BAR_ALPHA); } /** * 为滑动返回界面设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColorForSwipeBack(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { ViewGroup contentView = ((ViewGroup) activity.findViewById(android.R.id.content)); View rootView = contentView.getChildAt(0); int statusBarHeight = getStatusBarHeight(activity); if (rootView != null && rootView instanceof CoordinatorLayout) { final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) rootView; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { coordinatorLayout.setFitsSystemWindows(false); contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); boolean isNeedRequestLayout = contentView.getPaddingTop() < statusBarHeight; if (isNeedRequestLayout) { contentView.setPadding(0, statusBarHeight, 0, 0); coordinatorLayout.post(new Runnable() { @Override public void run() { coordinatorLayout.requestLayout(); } }); } } else { coordinatorLayout.setStatusBarBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } } else { contentView.setPadding(0, statusBarHeight, 0, 0); contentView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } setTransparentForWindow(activity); } } /** * 设置状态栏纯色 不加半透明效果 * * @param activity 需要设置的 activity * @param color 状态栏颜色值 */ public static void setColorNoTranslucent(Activity activity, @ColorInt int color) { setColor(activity, color, 0); } /** * 设置状态栏颜色(5.0以下无半透明效果,不建议使用) * * @param activity 需要设置的 activity * @param color 状态栏颜色值 */ @Deprecated public static void setColorDiff(Activity activity, @ColorInt int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); // 移除半透明矩形,以免叠加 View fakeStatusBarView = contentView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(color); } else { contentView.addView(createStatusBarView(activity, color)); } setRootView(activity); } /** * 使状态栏半透明 *

* 适用于图片作为背景的界面,此时需要图片填充到状态栏 * * @param activity 需要设置的activity */ public static void setTranslucent(Activity activity) { setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA); } /** * 使状态栏半透明 *

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

* 适用于图片作为背景的界面,此时需要图片填充到状态栏 * * @param activity 需要设置的activity * @param statusBarAlpha 状态栏透明度 */ public static void setTranslucentForCoordinatorLayout(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); addTranslucentView(activity, statusBarAlpha); } /** * 设置状态栏全透明 * * @param activity 需要设置的activity */ public static void setTransparent(Activity activity) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } transparentStatusBar(activity); setRootView(activity); } /** * 使状态栏透明(5.0以上半透明效果,不建议使用) *

* 适用于图片作为背景的界面,此时需要图片填充到状态栏 * * @param activity 需要设置的activity */ @Deprecated public static void setTranslucentDiff(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 设置状态栏透明 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); setRootView(activity); } } /** * 为DrawerLayout 布局设置状态栏变色 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout * @param color 状态栏颜色值 */ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { setColorForDrawerLayout(activity, drawerLayout, color, DEFAULT_STATUS_BAR_ALPHA); } /** * 为DrawerLayout 布局设置状态栏颜色,纯色 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout * @param color 状态栏颜色值 */ public static void setColorNoTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { setColorForDrawerLayout(activity, drawerLayout, color, 0); } /** * 为DrawerLayout 布局设置状态栏变色 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } // 生成一个状态栏大小的矩形 // 添加 statusBarView 到布局中 ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(color); } else { contentLayout.addView(createStatusBarView(activity, color), 0); } // 内容布局不是 LinearLayout 时,设置padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1) .setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(), contentLayout.getPaddingRight(), contentLayout.getPaddingBottom()); } // 设置属性 setDrawerLayoutProperty(drawerLayout, contentLayout); addTranslucentView(activity, statusBarAlpha); } /** * 设置 DrawerLayout 属性 * * @param drawerLayout DrawerLayout * @param drawerLayoutContentLayout DrawerLayout 的内容布局 */ private static void setDrawerLayoutProperty(DrawerLayout drawerLayout, ViewGroup drawerLayoutContentLayout) { ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1); drawerLayout.setFitsSystemWindows(false); drawerLayoutContentLayout.setFitsSystemWindows(false); drawerLayoutContentLayout.setClipToPadding(true); drawer.setFitsSystemWindows(false); } /** * 为DrawerLayout 布局设置状态栏变色(5.0以下无半透明效果,不建议使用) * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout * @param color 状态栏颜色值 */ @Deprecated public static void setColorForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout, @ColorInt int color) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 生成一个状态栏大小的矩形 ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); View fakeStatusBarView = contentLayout.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, DEFAULT_STATUS_BAR_ALPHA)); } else { // 添加 statusBarView 到布局中 contentLayout.addView(createStatusBarView(activity, color), 0); } // 内容布局不是 LinearLayout 时,设置padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // 设置属性 setDrawerLayoutProperty(drawerLayout, contentLayout); } } /** * 为 DrawerLayout 布局设置状态栏透明 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout */ public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { setTranslucentForDrawerLayout(activity, drawerLayout, DEFAULT_STATUS_BAR_ALPHA); } /** * 为 DrawerLayout 布局设置状态栏透明 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout */ public static void setTranslucentForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } setTransparentForDrawerLayout(activity, drawerLayout); addTranslucentView(activity, statusBarAlpha); } /** * 为 DrawerLayout 布局设置状态栏透明 * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout */ public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); // 内容布局不是 LinearLayout 时,设置padding top if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) { contentLayout.getChildAt(1).setPadding(0, getStatusBarHeight(activity), 0, 0); } // 设置属性 setDrawerLayoutProperty(drawerLayout, contentLayout); } /** * 为 DrawerLayout 布局设置状态栏透明(5.0以上半透明效果,不建议使用) * * @param activity 需要设置的activity * @param drawerLayout DrawerLayout */ @Deprecated public static void setTranslucentForDrawerLayoutDiff(Activity activity, DrawerLayout drawerLayout) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 设置状态栏透明 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 设置内容布局属性 ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0); contentLayout.setFitsSystemWindows(true); contentLayout.setClipToPadding(true); // 设置抽屉布局属性 ViewGroup vg = (ViewGroup) drawerLayout.getChildAt(1); vg.setFitsSystemWindows(false); // 设置 DrawerLayout 属性 drawerLayout.setFitsSystemWindows(false); } } /** * 为头部是 ImageView 的界面设置状态栏全透明 * * @param activity 需要设置的activity * @param needOffsetView 需要向下偏移的 View */ public static void setTransparentForImageView(Activity activity, View needOffsetView) { setTranslucentForImageView(activity, 0, needOffsetView); } /** * 为头部是 ImageView 的界面设置状态栏透明(使用默认透明度) * * @param activity 需要设置的activity * @param needOffsetView 需要向下偏移的 View */ public static void setTranslucentForImageView(Activity activity, View needOffsetView) { setTranslucentForImageView(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); } /** * 为头部是 ImageView 的界面设置状态栏透明 * * @param activity 需要设置的activity * @param statusBarAlpha 状态栏透明度 * @param needOffsetView 需要向下偏移的 View */ public static void setTranslucentForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View needOffsetView) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { return; } setTransparentForWindow(activity); addTranslucentView(activity, statusBarAlpha); if (needOffsetView != null) { Object haveSetOffset = needOffsetView.getTag(TAG_KEY_HAVE_SET_OFFSET); if (haveSetOffset != null && (Boolean) haveSetOffset) { return; } ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) needOffsetView.getLayoutParams(); layoutParams.setMargins(layoutParams.leftMargin, layoutParams.topMargin + getStatusBarHeight(activity), layoutParams.rightMargin, layoutParams.bottomMargin); needOffsetView.setTag(TAG_KEY_HAVE_SET_OFFSET, true); } } /** * 为 fragment 头部是 ImageView 的设置状态栏透明 * * @param activity fragment 对应的 activity * @param needOffsetView 需要向下偏移的 View */ public static void setTranslucentForImageViewInFragment(Activity activity, View needOffsetView) { setTranslucentForImageViewInFragment(activity, DEFAULT_STATUS_BAR_ALPHA, needOffsetView); } /** * 为 fragment 头部是 ImageView 的设置状态栏透明 * * @param activity fragment 对应的 activity * @param needOffsetView 需要向下偏移的 View */ public static void setTransparentForImageViewInFragment(Activity activity, View needOffsetView) { setTranslucentForImageViewInFragment(activity, 0, needOffsetView); } /** * 为 fragment 头部是 ImageView 的设置状态栏透明 * * @param activity fragment 对应的 activity * @param statusBarAlpha 状态栏透明度 * @param needOffsetView 需要向下偏移的 View */ public static void setTranslucentForImageViewInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View needOffsetView) { setTranslucentForImageView(activity, statusBarAlpha, needOffsetView); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { clearPreviousSetting(activity); } } /** * 隐藏伪状态栏 View * * @param activity 调用的 Activity */ public static void hideFakeStatusBarView(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { fakeStatusBarView.setVisibility(View.GONE); } View fakeTranslucentView = decorView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); if (fakeTranslucentView != null) { fakeTranslucentView.setVisibility(View.GONE); } } @TargetApi(Build.VERSION_CODES.M) public static void setLightMode(Activity activity) { setMIUIStatusBarDarkIcon(activity, true); setMeizuStatusBarDarkIcon(activity, true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } } @TargetApi(Build.VERSION_CODES.M) public static void setDarkMode(Activity activity) { setMIUIStatusBarDarkIcon(activity, false); setMeizuStatusBarDarkIcon(activity, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } } /** * 修改 MIUI V6 以上状态栏颜色 */ private static void setMIUIStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { Class clazz = activity.getWindow().getClass(); try { Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams"); Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE"); int darkModeFlag = field.getInt(layoutParams); Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class); extraFlagField.invoke(activity.getWindow(), darkIcon ? darkModeFlag : 0, darkModeFlag); } catch (Exception e) { //e.printStackTrace(); } } /** * 修改魅族状态栏字体颜色 Flyme 4.0 */ private static void setMeizuStatusBarDarkIcon(@NonNull Activity activity, boolean darkIcon) { try { WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON"); Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags"); darkFlag.setAccessible(true); meizuFlags.setAccessible(true); int bit = darkFlag.getInt(null); int value = meizuFlags.getInt(lp); if (darkIcon) { value |= bit; } else { value &= ~bit; } meizuFlags.setInt(lp, value); activity.getWindow().setAttributes(lp); } catch (Exception e) { //e.printStackTrace(); } } /////////////////////////////////////////////////////////////////////////////////// @TargetApi(Build.VERSION_CODES.KITKAT) private static void clearPreviousSetting(Activity activity) { ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { decorView.removeView(fakeStatusBarView); ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0); rootView.setPadding(0, 0, 0, 0); } } /** * 添加半透明矩形条 * * @param activity 需要设置的 activity * @param statusBarAlpha 透明值 */ private static void addTranslucentView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) { ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); View fakeTranslucentView = contentView.findViewById(FAKE_TRANSLUCENT_VIEW_ID); if (fakeTranslucentView != null) { if (fakeTranslucentView.getVisibility() == View.GONE) { fakeTranslucentView.setVisibility(View.VISIBLE); } fakeTranslucentView.setBackgroundColor(Color.argb(statusBarAlpha, 0, 0, 0)); } else { contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha)); } } /** * 生成一个和状态栏大小相同的彩色矩形条 * * @param activity 需要设置的 activity * @param color 状态栏颜色值 * @return 状态栏矩形条 */ private static View createStatusBarView(Activity activity, @ColorInt int color) { return createStatusBarView(activity, color, 0); } /** * 生成一个和状态栏大小相同的半透明矩形条 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param alpha 透明值 * @return 状态栏矩形条 */ private static View createStatusBarView(Activity activity, @ColorInt int color, int alpha) { // 绘制一个和状态栏一样高的矩形 View statusBarView = new View(activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(calculateStatusColor(color, alpha)); statusBarView.setId(FAKE_STATUS_BAR_VIEW_ID); return statusBarView; } /** * 设置根布局参数 */ private static void setRootView(Activity activity) { ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content); for (int i = 0, count = parent.getChildCount(); i < count; i++) { View childView = parent.getChildAt(i); if (childView instanceof ViewGroup) { childView.setFitsSystemWindows(true); ((ViewGroup) childView).setClipToPadding(true); } } } /** * 设置透明 */ private static void setTransparentForWindow(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().setStatusBarColor(Color.TRANSPARENT); activity.getWindow() .getDecorView() .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow() .setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * 使状态栏透明 */ @TargetApi(Build.VERSION_CODES.KITKAT) private static void transparentStatusBar(Activity activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); activity.getWindow().setStatusBarColor(Color.TRANSPARENT); } else { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * 创建半透明矩形 View * * @param alpha 透明值 * @return 半透明 View */ private static View createTranslucentStatusBarView(Activity activity, int alpha) { // 绘制一个和状态栏一样高的矩形 View statusBarView = new View(activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(Color.argb(alpha, 0, 0, 0)); statusBarView.setId(FAKE_TRANSLUCENT_VIEW_ID); return statusBarView; } /** * 获取状态栏高度 * * @param context context * @return 状态栏高度 */ private static int getStatusBarHeight(Context context) { // 获得状态栏高度 int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); return context.getResources().getDimensionPixelSize(resourceId); } /** * 计算状态栏颜色 * * @param color color值 * @param alpha alpha值 * @return 最终的状态栏颜色 */ private static int calculateStatusColor(@ColorInt int color, int alpha) { if (alpha == 0) { return color; } float a = 1 - alpha / 255f; int red = color >> 16 & 0xff; int green = color >> 8 & 0xff; int blue = color & 0xff; red = (int) (red * a + 0.5); green = (int) (green * a + 0.5); blue = (int) (blue * a + 0.5); return 0xff << 24 | red << 16 | green << 8 | blue; } } ================================================ FILE: library/src/main/res/values/ids.xml ================================================ ================================================ FILE: library/src/main/res/values/strings.xml ================================================ ================================================ FILE: sample/.gitignore ================================================ /build ================================================ FILE: sample/build.gradle ================================================ apply plugin: 'com.android.application' android { compileSdkVersion 27 buildToolsVersion "27.0.3" defaultConfig { applicationId "com.jaeger.statusbardemo" minSdkVersion 14 targetSdkVersion 27 versionCode 8 versionName "1.5.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:27.1.0' compile 'com.android.support:design:27.1.0' compile 'com.ashokvarma.android:bottom-navigation-bar:1.3.0' compile 'com.r0adkll:slidableactivity:2.0.5' compile project(path: ':library') // compile 'com.jaeger.statusbaruitl:library:1.3.2' } ================================================ FILE: sample/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # By default, the flags in this file are appended to flags specified # in /Users/Jaeger/Develop/android/sdk/tools/proguard/proguard-android.txt # You can edit the include path and order by changing the proguardFiles # directive in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # Add any project specific keep options here: # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} ================================================ FILE: sample/release/output.json ================================================ [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":6},"path":"sample-release.apk","properties":{"packageId":"com.jaeger.statusbardemo","split":"","minSdkVersion":"14"}}] ================================================ FILE: sample/src/main/AndroidManifest.xml ================================================ ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/BaseActivity.java ================================================ package com.jaeger.statusbarutil; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; /** * Created by Jaeger on 16/2/14. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class BaseActivity extends AppCompatActivity { @Override public void setContentView(int layoutResID) { super.setContentView(layoutResID); setStatusBar(); } protected void setStatusBar() { StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary)); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return super.onOptionsItemSelected(item); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/ColorStatusBarActivity.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.TextView; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; import java.util.Random; /** * Created by Jaeger on 16/2/14. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class ColorStatusBarActivity extends BaseActivity { private Toolbar mToolbar; private Button mBtnChangeColor; private SeekBar mSbChangeAlpha; private TextView mTvStatusAlpha; private int mColor; private int mAlpha; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_color_status_bar); mToolbar = findViewById(R.id.toolbar); mBtnChangeColor = findViewById(R.id.btn_change_color); mTvStatusAlpha = findViewById(R.id.tv_status_alpha); mSbChangeAlpha = findViewById(R.id.sb_change_alpha); // 设置toolbar setSupportActionBar(mToolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } // 改变颜色 mBtnChangeColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Random random = new Random(); mColor = 0xff000000 | random.nextInt(0xffffff); mToolbar.setBackgroundColor(mColor); StatusBarUtil.setColor(ColorStatusBarActivity.this, mColor, mAlpha); } }); mSbChangeAlpha.setMax(255); mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mAlpha = progress; StatusBarUtil.setColor(ColorStatusBarActivity.this, mColor, mAlpha); mTvStatusAlpha.setText(String.valueOf(mAlpha)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); } @Override protected void setStatusBar() { mColor = getResources().getColor(R.color.colorPrimary); StatusBarUtil.setColor(this, mColor); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/ImageFragment.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.jaeger.statusbardemo.R; /** * Created by Jaeger on 16/8/11. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class ImageFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragement_image, container, false); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/ImageStatusBarActivity.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; /** * Created by Jaeger on 16/2/14. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class ImageStatusBarActivity extends BaseActivity { public static final String EXTRA_IS_TRANSPARENT = "is_transparent"; private TextView mTvStatusAlpha; private RelativeLayout mRootLayout; private Button mBtnChangeBackground; private boolean isBgChanged; private SeekBar mSbChangeAlpha; private boolean isTransparent; private int mAlpha; @Override protected void onCreate(Bundle savedInstanceState) { isTransparent = getIntent().getBooleanExtra(EXTRA_IS_TRANSPARENT, false); super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_status_bar); mRootLayout = (RelativeLayout) findViewById(R.id.root_layout); mBtnChangeBackground = (Button) findViewById(R.id.btn_change_background); mTvStatusAlpha = (TextView) findViewById(R.id.tv_status_alpha); mSbChangeAlpha = (SeekBar) findViewById(R.id.sb_change_alpha); mBtnChangeBackground.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isBgChanged = !isBgChanged; if (isBgChanged) { mRootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_girl)); } else { mRootLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_monkey)); } } }); if (!isTransparent) { mSbChangeAlpha.setVisibility(View.VISIBLE); setSeekBar(); } else { mSbChangeAlpha.setVisibility(View.GONE); } } @Override protected void setStatusBar() { if (isTransparent) { StatusBarUtil.setTransparent(this); } else { StatusBarUtil.setTranslucent(this, StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); } } private void setSeekBar() { mSbChangeAlpha.setMax(255); mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mAlpha = progress; StatusBarUtil.setTranslucent(ImageStatusBarActivity.this, mAlpha); mTvStatusAlpha.setText(String.valueOf(mAlpha)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/ImageViewActivity.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; import com.r0adkll.slidr.Slidr; /** * Created by Jaeger on 16/7/12. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class ImageViewActivity extends BaseActivity { private Toolbar mToolbar; private View mViewNeedOffset; private SeekBar mSbChangeAlpha; private TextView mTvStatusAlpha; private int mAlpha; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_view); // 设置右滑动返回 Slidr.attach(this); mToolbar = (Toolbar) findViewById(R.id.toolbar); mViewNeedOffset = findViewById(R.id.view_need_offset); mTvStatusAlpha = (TextView) findViewById(R.id.tv_status_alpha); mSbChangeAlpha = (SeekBar) findViewById(R.id.sb_change_alpha); setSupportActionBar(mToolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } mSbChangeAlpha.setMax(255); mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mAlpha = progress; StatusBarUtil.setTranslucentForImageView(ImageViewActivity.this, mAlpha, mViewNeedOffset); mTvStatusAlpha.setText(String.valueOf(mAlpha)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); } @Override protected void setStatusBar() { mViewNeedOffset = findViewById(R.id.view_need_offset); StatusBarUtil.setTranslucentForImageView(this, mViewNeedOffset); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { finish(); } return super.onOptionsItemSelected(item); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/MainActivity.java ================================================ package com.jaeger.statusbarutil; import android.content.Intent; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.CheckBox; import android.widget.SeekBar; import android.widget.TextView; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; /** * Created by Jaeger on 16/2/14. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class MainActivity extends BaseActivity { private DrawerLayout mDrawerLayout; private Toolbar mToolbar; private CheckBox mChbTranslucent; private Button mBtnSetColor; private Button mBtnSetTransparent; private Button mBtnSetTranslucent; private Button mBtnSetForImageView; private Button mBtnUseInFragment; private Button mBtnSetColorForSwipeBack; private Button mBtnSwitchMode; private ViewGroup contentLayout; private SeekBar mSbChangeAlpha; private TextView mTvStatusAlpha; private int mStatusBarColor; private int mAlpha = StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDrawerLayout = findViewById(R.id.drawer_layout); contentLayout = findViewById(R.id.main); mToolbar = findViewById(R.id.toolbar); mChbTranslucent = findViewById(R.id.chb_translucent); mBtnSetColor = findViewById(R.id.btn_set_color); mBtnSetTransparent = findViewById(R.id.btn_set_transparent); mBtnSetTranslucent = findViewById(R.id.btn_set_translucent); mBtnSetForImageView = findViewById(R.id.btn_set_for_image_view); mBtnUseInFragment = findViewById(R.id.btn_use_in_fragment); mBtnSetColorForSwipeBack = findViewById(R.id.btn_set_color_for_swipe_back); mBtnSwitchMode = findViewById(R.id.btn_switch_mode); mSbChangeAlpha = findViewById(R.id.sb_change_alpha); mTvStatusAlpha = findViewById(R.id.tv_status_alpha); setSupportActionBar(mToolbar); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); mDrawerLayout.setDrawerListener(toggle); toggle.syncState(); mBtnSetColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ColorStatusBarActivity.class); startActivity(intent); } }); mBtnSetTransparent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ImageStatusBarActivity.class); intent.putExtra(ImageStatusBarActivity.EXTRA_IS_TRANSPARENT, true); startActivity(intent); } }); mBtnSetTranslucent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ImageStatusBarActivity.class); intent.putExtra(ImageStatusBarActivity.EXTRA_IS_TRANSPARENT, false); startActivity(intent); } }); mBtnSetForImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, ImageViewActivity.class); startActivity(intent); } }); mBtnUseInFragment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, UseInFragmentActivity.class); startActivity(intent); } }); mBtnSetColorForSwipeBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SwipeBackActivity.class); startActivity(intent); } }); mBtnSwitchMode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, SwitchModeActivity.class); startActivity(intent); } }); mChbTranslucent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mChbTranslucent.isChecked()) { contentLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_monkey)); StatusBarUtil.setTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, mAlpha); mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent)); } else { contentLayout.setBackgroundDrawable(null); mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary)); StatusBarUtil.setColorForDrawerLayout(MainActivity.this, mDrawerLayout, getResources().getColor(R.color.colorPrimary), mAlpha); } } }); mSbChangeAlpha.setMax(255); mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { mAlpha = progress; if (mChbTranslucent.isChecked()) { StatusBarUtil.setTranslucentForDrawerLayout(MainActivity.this, mDrawerLayout, mAlpha); } else { StatusBarUtil.setColorForDrawerLayout(MainActivity.this, mDrawerLayout, mStatusBarColor, mAlpha); } mTvStatusAlpha.setText(String.valueOf(mAlpha)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA); } @Override protected void setStatusBar() { mStatusBarColor = getResources().getColor(R.color.colorPrimary); StatusBarUtil.setColorForDrawerLayout(this, (DrawerLayout) findViewById(R.id.drawer_layout), mStatusBarColor, mAlpha); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/SimpleFragment.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.annotation.ColorInt; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.jaeger.statusbardemo.R; /** * Created by Jaeger on 16/8/11. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class SimpleFragment extends Fragment { private TextView mTvTitle; private View mFakeStatusBar; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragement_simple, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); mTvTitle = (TextView) view.findViewById(R.id.tv_title); mFakeStatusBar = view.findViewById(R.id.fake_status_bar); } public void setTvTitleBackgroundColor(@ColorInt int color) { mTvTitle.setBackgroundColor(color); mFakeStatusBar.setBackgroundColor(color); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/SwipeBackActivity.java ================================================ package com.jaeger.statusbarutil; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; import com.r0adkll.slidr.Slidr; import java.util.Random; /** * Created by Jaeger on 2016/10/15. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class SwipeBackActivity extends BaseActivity { private Button mBtnChangeColor; private int mColor = Color.GRAY; private Toolbar mToolbar; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置右滑动返回 Slidr.attach(this); setContentView(R.layout.swipe_back_activity); mToolbar = (Toolbar) findViewById(R.id.toolbar); mBtnChangeColor = (Button) findViewById(R.id.btn_change_color); setSupportActionBar(mToolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } mToolbar.setBackgroundColor(mColor); mBtnChangeColor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Random random = new Random(); mColor = 0xff000000 | random.nextInt(0xffffff); mToolbar.setBackgroundColor(mColor); StatusBarUtil.setColorForSwipeBack(SwipeBackActivity.this, mColor, 38); } }); } @Override protected void setStatusBar() { StatusBarUtil.setColorForSwipeBack(this, mColor, 38); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/SwitchModeActivity.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.Button; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; /** * Created by jaeger on 08/03/2018. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class SwitchModeActivity extends BaseActivity { private Toolbar mToolbar; private Button mBtnSetLightMode; private Button mBtnSetDarkMode; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_switch_mode); mToolbar = findViewById(R.id.toolbar); mBtnSetLightMode = findViewById(R.id.btn_set_light_mode); mBtnSetDarkMode = findViewById(R.id.btn_set_dark_mode); // 设置toolbar setSupportActionBar(mToolbar); if (getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } mBtnSetLightMode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int color = getResources().getColor(R.color.light_gray); StatusBarUtil.setColor(SwitchModeActivity.this, color, 30); mToolbar.setBackgroundColor(color); StatusBarUtil.setLightMode(SwitchModeActivity.this); } }); mBtnSetDarkMode.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int color = getResources().getColor(R.color.colorPrimary); StatusBarUtil.setColor(SwitchModeActivity.this, color); mToolbar.setBackgroundColor(color); StatusBarUtil.setDarkMode(SwitchModeActivity.this); } }); } @Override protected void setStatusBar() { StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary)); } } ================================================ FILE: sample/src/main/java/com/jaeger/statusbarutil/UseInFragmentActivity.java ================================================ package com.jaeger.statusbarutil; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import com.ashokvarma.bottomnavigation.BottomNavigationBar; import com.ashokvarma.bottomnavigation.BottomNavigationItem; import com.jaeger.library.StatusBarUtil; import com.jaeger.statusbardemo.R; import java.util.ArrayList; import java.util.Random; /** * Created by Jaeger on 16/8/11. * * Email: chjie.jaeger@gmail.com * GitHub: https://github.com/laobie */ public class UseInFragmentActivity extends BaseActivity { private ViewPager mVpHome; private BottomNavigationBar mBottomNavigationBar; private ArrayList mFragmentList = new ArrayList<>(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_use_in_fragment); mVpHome = (ViewPager) findViewById(R.id.vp_home); mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar); mBottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_favorite, "One")) .addItem(new BottomNavigationItem(R.drawable.ic_gavel, "Two")) .addItem(new BottomNavigationItem(R.drawable.ic_grade, "Three")) .addItem(new BottomNavigationItem(R.drawable.ic_group_work, "Four")) .initialise(); mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() { @Override public void onTabSelected(int position) { mVpHome.setCurrentItem(position); } @Override public void onTabUnselected(int position) { } @Override public void onTabReselected(int position) { } }); mFragmentList.add(new ImageFragment()); mFragmentList.add(new SimpleFragment()); mFragmentList.add(new SimpleFragment()); mFragmentList.add(new SimpleFragment()); mVpHome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { mBottomNavigationBar.selectTab(position); switch (position) { case 0: break; default: Random random = new Random(); int color = 0xff000000 | random.nextInt(0xffffff); if (mFragmentList.get(position) instanceof SimpleFragment) { ((SimpleFragment) mFragmentList.get(position)).setTvTitleBackgroundColor(color); } break; } } @Override public void onPageScrollStateChanged(int state) { } }); mVpHome.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int position) { return mFragmentList.get(position); } @Override public int getCount() { return mFragmentList.size(); } }); } @Override protected void setStatusBar() { StatusBarUtil.setTranslucentForImageViewInFragment(UseInFragmentActivity.this, null); } } ================================================ FILE: sample/src/main/res/anim/slide_in_left.xml ================================================ ================================================ FILE: sample/src/main/res/anim/slide_in_right.xml ================================================ ================================================ FILE: sample/src/main/res/anim/slide_out_left.xml ================================================ ================================================ FILE: sample/src/main/res/anim/slide_out_right.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_camera.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_gallery.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_manage.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_send.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_share.xml ================================================ ================================================ FILE: sample/src/main/res/drawable/ic_menu_slideshow.xml ================================================ ================================================ FILE: sample/src/main/res/layout/activity_color_status_bar.xml ================================================