diff --git a/README.md b/README.md index d14c949..b1c47cc 100644 --- a/README.md +++ b/README.md @@ -23,8 +23,9 @@ Add the dependency ```groovy dependencies { // After AndroidX - implementation ('com.github.niorgai:StatusBarCompat:2.2.0', { - exclude group: 'com.android.support' + implementation ('com.github.niorgai:StatusBarCompat:2.3.0', { + exclude group: 'androidx.appcompat:appcompat' + exclude group: 'com.google.android.material:material' }) // Before AndroidX compile ('com.github.niorgai:StatusBarCompat:2.1.4', { @@ -46,7 +47,8 @@ TranslucentStatusBar | SetStatusBarColor --- This is a utils for status bar, And you **do not need to** change your `style.xml` file. -The most feature is **you can toggle `setStatusBarColor` and `translucentStatusBar` without recreate activity**. + +The most important feature is **you can toggle `setStatusBarColor` and `translucentStatusBar` without recreate activity**. ```java //set color for status bar @@ -63,12 +65,18 @@ The most feature is **you can toggle `setStatusBarColor` and `translucentStatusB setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor) + //Change to Light status bar (convert icon and text color to dark, SDK >= 23) + changeToLightStatusBar(Activity activity) + + //Cancel Light status bar (convert icon and text color to light) + cancelLightStatusBar(Activity activity) ``` # How to use 1. Use it after call `setContentView` 2. If use FullScreen Activity, must call `StatusBarCompat.translucentStatusBar(activity);` +3. If you want to set light status bar, Remember call `changeToLightStatusBar()` at last. # About Demo: Demo shows One Activity with four Fragments. @@ -77,6 +85,7 @@ Demo shows One Activity with four Fragments. 2. TranslucentFragment call `translucentStatusBar` method, when sdk >= 21, button can toggle hide/show statusBar's shadow. 3. DrawerFragment call `setStatusBarColor` method, show how to compat for DrawerLayout and CoordinatorLayout. 4. CollapsingFragment call `setStatusBarColorForCollapsingToolbar` method, compat for CollapsingToolbarLayout. +5. Each Fragment can toggle Light status bar with Checkbox. ## Issues: These problem only show in SDK between 19 and 21: @@ -112,11 +121,18 @@ StatusBarCompat.translucentStatusBar(Activity activity, boolean hideStatusBarBac //为 CollapsingToolbarLayout 设置颜色 setStatusBarColorForCollapsingToolbar(Activity activity, AppBarLayout appBarLayout, CollapsingToolbarLayout collapsingToolbarLayout, Toolbar toolbar, int statusColor) + +//SDK >= 23, 将状态栏改为浅色模式(状态栏 icon 和字体会变成深色) +changeToLightStatusBar(Activity activity) + +//将状态栏改为深色模式(状态栏 icon 和字体会变成浅色, 即默认模式) +cancelLightStatusBar(Activity activity) ``` # 怎么使用 1. 在 `setContentView` 方法调用后再设置. -2. 如果使用了全屏 Activity ,记得调用`StatusBarCompat.translucentStatusBar(activity);` +2. 如果要使用全屏 Activity , 调用 `StatusBarCompat.translucentStatusBar(activity)` . +3. 如果想让状态栏图标变成深色, 记得**最后**调用 `changeToLightStatusBar(activity)` . # Demo 说明: Demo 只有一个 Activity , 分四个 Tab. @@ -125,6 +141,7 @@ Demo 只有一个 Activity , 分四个 Tab. 2. TranslucentFragment 为图片布局, 展示透明状态栏效果, SDK >= 21时, 可以通过按钮显示\隐藏 statusBar 的 shadow, 调用方法为: `translucentStatusBar`. 3. DrawerFragment 展示 DrawerLayout + CoordinatorLayout, 调用方法为: `setStatusBarColor`. 4. CollapsingFragment 展示 CollapsingLayout 的适配效果, 调用方法为: `setStatusBarColorForCollapsingToolbar`. +5. 每个 Fragment 下都可以通过 Checkbox 切换浅色/深色状态栏. ## 已知问题 这些问题仅仅出现在 4.4 上 diff --git a/app/build.gradle b/app/build.gradle index 1ca643e..a6929b5 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -27,7 +27,7 @@ dependencies { testImplementation 'junit:junit:4.12' implementation project(path: ':library') implementation "androidx.appcompat:appcompat:${lib.androidXVersion}" - implementation "com.google.android.material:material:${lib.androidXVersion}" - implementation "androidx.legacy:legacy-support-v4:${lib.androidXVersion}" - implementation "androidx.cardview:cardview:${lib.androidXVersion}" + implementation "com.google.android.material:material:${lib.materialVersion}" + implementation "androidx.legacy:legacy-support-v4:${lib.materialVersion}" + implementation "androidx.cardview:cardview:${lib.materialVersion}" } diff --git a/app/src/main/java/qiu/statusbarcompat/fragment/BaseFragment.java b/app/src/main/java/qiu/statusbarcompat/fragment/BaseFragment.java new file mode 100644 index 0000000..7ba74fd --- /dev/null +++ b/app/src/main/java/qiu/statusbarcompat/fragment/BaseFragment.java @@ -0,0 +1,43 @@ +package qiu.statusbarcompat.fragment; + +import android.os.Bundle; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import qiu.niorgai.StatusBarCompat; +import qiu.statusbarcompat.R; + +/** + * Created by jianqiu on 12/12/18. + */ +public class BaseFragment extends Fragment implements CompoundButton.OnCheckedChangeListener { + + protected CheckBox mCheckBox; + + protected void initCheckBox() { + if (getView() == null) { + return; + } + mCheckBox = getView().findViewById(R.id.light_status_bar); + mCheckBox.setOnCheckedChangeListener(this); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + initCheckBox(); + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + StatusBarCompat.changeToLightStatusBar(getActivity()); + } else { + StatusBarCompat.cancelLightStatusBar(getActivity()); + } + } +} diff --git a/app/src/main/java/qiu/statusbarcompat/fragment/CollapsingToolbarFragment.java b/app/src/main/java/qiu/statusbarcompat/fragment/CollapsingToolbarFragment.java index 22ae10f..a1d9772 100644 --- a/app/src/main/java/qiu/statusbarcompat/fragment/CollapsingToolbarFragment.java +++ b/app/src/main/java/qiu/statusbarcompat/fragment/CollapsingToolbarFragment.java @@ -2,16 +2,16 @@ import android.graphics.Color; import android.os.Bundle; -import androidx.annotation.Nullable; -import com.google.android.material.appbar.AppBarLayout; -import com.google.android.material.appbar.CollapsingToolbarLayout; -import androidx.fragment.app.Fragment; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import com.google.android.material.appbar.AppBarLayout; +import com.google.android.material.appbar.CollapsingToolbarLayout; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; import qiu.niorgai.StatusBarCompat; import qiu.statusbarcompat.R; @@ -19,7 +19,7 @@ * Compat CollapsingToolbarLayout * Created by qiu on 7/27/16. */ -public class CollapsingToolbarFragment extends Fragment { +public class CollapsingToolbarFragment extends BaseFragment { public CollapsingToolbarFragment() { diff --git a/app/src/main/java/qiu/statusbarcompat/fragment/CommonFragment.java b/app/src/main/java/qiu/statusbarcompat/fragment/CommonFragment.java index 49ede91..8e0ed84 100644 --- a/app/src/main/java/qiu/statusbarcompat/fragment/CommonFragment.java +++ b/app/src/main/java/qiu/statusbarcompat/fragment/CommonFragment.java @@ -2,13 +2,12 @@ import android.graphics.Color; import android.os.Bundle; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.SeekBar; +import androidx.annotation.Nullable; import qiu.niorgai.StatusBarCompat; import qiu.statusbarcompat.R; import qiu.statusbarcompat.activity.MainActivity; @@ -16,7 +15,7 @@ /** * Common Layout, test for setStatusBarColor */ -public class CommonFragment extends Fragment { +public class CommonFragment extends BaseFragment { public CommonFragment() { diff --git a/app/src/main/java/qiu/statusbarcompat/fragment/DrawerFragment.java b/app/src/main/java/qiu/statusbarcompat/fragment/DrawerFragment.java index 40e9010..ec52405 100644 --- a/app/src/main/java/qiu/statusbarcompat/fragment/DrawerFragment.java +++ b/app/src/main/java/qiu/statusbarcompat/fragment/DrawerFragment.java @@ -1,22 +1,21 @@ package qiu.statusbarcompat.fragment; import android.os.Bundle; -import androidx.annotation.Nullable; -import androidx.fragment.app.Fragment; -import androidx.core.content.ContextCompat; -import androidx.appcompat.app.AppCompatActivity; -import androidx.appcompat.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; import qiu.niorgai.StatusBarCompat; import qiu.statusbarcompat.R; /** * Test for DrawerLayout and CoordinatorLayout */ -public class DrawerFragment extends Fragment { +public class DrawerFragment extends BaseFragment { public DrawerFragment() { diff --git a/app/src/main/java/qiu/statusbarcompat/fragment/TranslucentFragment.java b/app/src/main/java/qiu/statusbarcompat/fragment/TranslucentFragment.java index 5773ed8..e0223da 100644 --- a/app/src/main/java/qiu/statusbarcompat/fragment/TranslucentFragment.java +++ b/app/src/main/java/qiu/statusbarcompat/fragment/TranslucentFragment.java @@ -14,7 +14,7 @@ /** * Test for translucent */ -public class TranslucentFragment extends Fragment { +public class TranslucentFragment extends BaseFragment { private boolean isHide = false; diff --git a/app/src/main/res/layout/fragment_collapsing_tool_bar.xml b/app/src/main/res/layout/fragment_collapsing_tool_bar.xml index 999f0a6..6d6d5b4 100644 --- a/app/src/main/res/layout/fragment_collapsing_tool_bar.xml +++ b/app/src/main/res/layout/fragment_collapsing_tool_bar.xml @@ -154,6 +154,17 @@ + + + + diff --git a/app/src/main/res/layout/fragment_drawer.xml b/app/src/main/res/layout/fragment_drawer.xml index dac9103..3796bfe 100644 --- a/app/src/main/res/layout/fragment_drawer.xml +++ b/app/src/main/res/layout/fragment_drawer.xml @@ -119,6 +119,17 @@ + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index bc015b8..97147fa 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.2.0' + classpath 'com.android.tools.build:gradle:3.2.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -33,7 +33,8 @@ ext { ] libs = [ - androidXVersion : "1.0.0" + androidXVersion : "1.0.2", + materialVersion : "1.0.0" ] } diff --git a/library/build.gradle b/library/build.gradle index 08a7135..a03e952 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -25,5 +25,5 @@ dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' implementation "androidx.appcompat:appcompat:${lib.androidXVersion}" - implementation "com.google.android.material:material:${lib.androidXVersion}" + implementation "com.google.android.material:material:${lib.materialVersion}" } diff --git a/library/src/main/java/qiu/niorgai/StatusBarCompat.java b/library/src/main/java/qiu/niorgai/StatusBarCompat.java index ef4af8f..fdf751d 100644 --- a/library/src/main/java/qiu/niorgai/StatusBarCompat.java +++ b/library/src/main/java/qiu/niorgai/StatusBarCompat.java @@ -2,10 +2,14 @@ import android.app.Activity; import android.os.Build; -import androidx.annotation.ColorInt; -import androidx.annotation.NonNull; +import android.view.View; +import android.view.Window; + import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; + +import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; /** @@ -67,4 +71,40 @@ public static void setStatusBarColorForCollapsingToolbar(@NonNull Activity activ StatusBarCompatKitKat.setStatusBarColorForCollapsingToolbar(activity, appBarLayout, collapsingToolbarLayout, toolbar, statusColor); } } + + public static void changeToLightStatusBar(@NonNull Activity activity) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return; + } + if (activity == null) { + return; + } + Window window = activity.getWindow(); + if (window == null) { + return; + } + View decorView = window.getDecorView(); + if (decorView == null) { + return; + } + decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); + } + + public static void cancelLightStatusBar(@NonNull Activity activity) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + return; + } + if (activity == null) { + return; + } + Window window = activity.getWindow(); + if (window == null) { + return; + } + View decorView = window.getDecorView(); + if (decorView == null) { + return; + } + decorView.setSystemUiVisibility(decorView.getSystemUiVisibility() ^ View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); + } }