1
1
package com .nanchen .rxjava2examples .base ;
2
2
3
+ import android .graphics .Color ;
4
+ import android .os .Build ;
3
5
import android .os .Bundle ;
4
6
import android .support .annotation .LayoutRes ;
5
7
import android .support .annotation .Nullable ;
6
8
import android .support .v7 .app .AppCompatActivity ;
7
- import android .support .v7 .widget .Toolbar ;
8
9
import android .view .View ;
9
- import android .widget .TextView ;
10
+ import android .view .Window ;
11
+ import android .view .WindowManager ;
10
12
11
13
import com .nanchen .rxjava2examples .R ;
14
+ import com .nanchen .rxjava2examples .ui .SystemBarTintManager ;
12
15
13
- import butterknife .BindView ;
14
16
import butterknife .ButterKnife ;
15
17
16
18
/**
17
- * 封装一个带ToolBar和Butterknife注解的Activity基类
19
+ * Activity基类
18
20
*
19
21
* Author: nanchen
20
22
21
- * Date: 2017-06-19 13:51
23
+ * Date: 2017-06-20 14:21
22
24
*/
23
25
24
26
public abstract class BaseActivity extends AppCompatActivity {
25
27
26
- @ BindView (R .id .toolbar )
27
- Toolbar mToolbar ;
28
- @ BindView (R .id .title_text )
29
- TextView mTitleName ;
30
-
31
28
/**
32
29
* 获取布局ID
33
30
*
@@ -49,11 +46,6 @@ protected void beforeInit() {
49
46
*/
50
47
protected abstract void initView (Bundle savedInstanceState );
51
48
52
- /**
53
- * 设置标题文本
54
- */
55
- protected abstract String getSubTitle ();
56
-
57
49
@ Override
58
50
public void setContentView (@ LayoutRes int layoutResID ) {
59
51
super .setContentView (layoutResID );
@@ -63,42 +55,51 @@ public void setContentView(@LayoutRes int layoutResID) {
63
55
@ Override
64
56
protected void onCreate (@ Nullable Bundle savedInstanceState ) {
65
57
super .onCreate (savedInstanceState );
58
+ initSystemBarTint ();
66
59
beforeInit ();
67
60
if (getContentViewLayoutID () != 0 ) {
68
61
setContentView (getContentViewLayoutID ());
69
- initToolbar ();
70
62
initView (savedInstanceState );
71
63
}
72
64
}
73
65
74
- private void initToolbar () {
75
- if (mToolbar != null ){
76
- mToolbar .setTitle ("" );
77
- mTitleName .setText (getSubTitle ());
78
- if (isShowBack ()){
79
- showBack ();
80
- }
81
- }
66
+ /** 子类可以重写决定是否使用透明状态栏 */
67
+ protected boolean translucentStatusBar () {
68
+ return false ;
82
69
}
83
70
84
- /**
85
- * 版本号小于21的后退按钮图片
86
- */
87
- private void showBack (){
88
- //setNavigationIcon必须在setSupportActionBar(toolbar);方法后面加入
89
- setSupportActionBar (mToolbar );
90
- mToolbar .setNavigationIcon (R .mipmap .icon_back );
91
- mToolbar .setNavigationOnClickListener (new View .OnClickListener () {
92
- @ Override
93
- public void onClick (View v ) {
94
- onBackPressed ();
71
+ /** 设置状态栏颜色 */
72
+ protected void initSystemBarTint () {
73
+ Window window = getWindow ();
74
+ if (translucentStatusBar ()) {
75
+ // 设置状态栏全透明
76
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
77
+ window .clearFlags (WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS );
78
+ window .getDecorView ().setSystemUiVisibility (View .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View .SYSTEM_UI_FLAG_LAYOUT_STABLE );
79
+ window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
80
+ window .setStatusBarColor (Color .TRANSPARENT );
81
+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
82
+ getWindow ().addFlags (WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS );
95
83
}
96
- });
84
+ return ;
85
+ }
86
+ // 沉浸式状态栏
87
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
88
+ //5.0以上使用原生方法
89
+ window .clearFlags (WindowManager .LayoutParams .FLAG_TRANSLUCENT_STATUS );
90
+ window .addFlags (WindowManager .LayoutParams .FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS );
91
+ window .setStatusBarColor (setStatusBarColor ());
92
+ } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
93
+ //4.4-5.0使用三方工具类,有些4.4的手机有问题,这里为演示方便,不使用沉浸式
94
+ // getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
95
+ SystemBarTintManager tintManager = new SystemBarTintManager (this );
96
+ tintManager .setStatusBarTintEnabled (true );
97
+ tintManager .setStatusBarTintColor (setStatusBarColor ());
98
+ }
97
99
}
98
100
99
-
100
- protected boolean isShowBack () {
101
- return true ;
101
+ /** 子类可以重写改变状态栏颜色 */
102
+ protected int setStatusBarColor () {
103
+ return R . color . blue ;
102
104
}
103
-
104
105
}
0 commit comments