Browse Source

添加状态栏工具

316044749 7 years ago
parent
commit
c27b8597cc

+ 4 - 0
app/build.gradle

@@ -8,6 +8,7 @@ android {
         targetSdkVersion 26
         versionCode 1
         versionName "1.0"
+        javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }
     }
     buildTypes {
         release {
@@ -21,4 +22,7 @@ dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile project(':library')
     compile 'com.android.support:appcompat-v7:26.1.0'
+    compile 'com.android.support:design:26.1.0'
+    compile 'com.jakewharton:butterknife:7.0.1'
+    compile 'com.github.bumptech.glide:glide:3.7.0'
 }

+ 10 - 4
app/src/main/java/com/ynstkz/shitu/android/activity/HomeActivity.java

@@ -1,21 +1,27 @@
 package com.ynstkz.shitu.android.activity;
 
 import android.os.Bundle;
+import android.widget.TextView;
 
 import com.ynstkz.shitu.android.R;
-import com.ynstkz.shitu.android.base.BaseActivity;
 import com.ynstkz.shitu.android.base.TitleBarActivity;
 
+import butterknife.Bind;
+import butterknife.ButterKnife;
+
 public class HomeActivity extends TitleBarActivity {
 
+    @Bind(R.id.tv_barTitle)
+    TextView tvBarTitle;
+
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_home);
+        ButterKnife.bind(this);
     }
 
     @Override
-    protected String getBarTitle() {
-        return "测试";
+    protected int getLayoutId() {
+        return R.layout.activity_home;
     }
 }

+ 3 - 1
app/src/main/java/com/ynstkz/shitu/android/base/BaseActivity.java

@@ -4,6 +4,9 @@ import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v7.app.AppCompatActivity;
 
+import com.ynstkz.shitu.android.R;
+import com.ynstkz.shitu.android.utils.StatusBarUtil;
+
 /**
  * 作者:fuchangle on 2018/1/27 21:50
  */
@@ -14,5 +17,4 @@ public class BaseActivity extends AppCompatActivity{
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     }
-    
 }

+ 42 - 19
app/src/main/java/com/ynstkz/shitu/android/base/TitleBarActivity.java

@@ -1,10 +1,13 @@
 package com.ynstkz.shitu.android.base;
 
 import android.os.Bundle;
-import android.os.PersistableBundle;
+import android.support.annotation.Nullable;
+import android.support.design.widget.Snackbar;
 import android.view.View;
+import android.widget.TextView;
 
-import com.ynstkz.shitu.android.helper.TitleBarHelper;
+import com.ynstkz.shitu.android.R;
+import com.ynstkz.shitu.android.utils.StatusBarUtil;
 
 /**
  * 作者:fuchangle on 2018/1/27 22:25
@@ -12,34 +15,54 @@ import com.ynstkz.shitu.android.helper.TitleBarHelper;
 
 public abstract class TitleBarActivity extends BaseActivity{
 
-    private TitleBarHelper titleBarHelper;
+    private TextView mToolbarTitle;
 
     @Override
-    protected void onCreate(Bundle savedInstanceState) {
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        setContentView(getLayoutId());
+        StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
+        mToolbarTitle = findViewById(R.id.tv_title);
+        if (mToolbarTitle != null) {
+            //getTitle()的值是activity的android:lable属性值
+            mToolbarTitle.setText(getTitle());
+            //设置默认的标题不显示
+            if(getSupportActionBar() != null){
+                getSupportActionBar().setDisplayShowTitleEnabled(false);
+            }
+        }
     }
 
-    @Override
-    public void setContentView(int layoutResID) {
-        titleBarHelper = new TitleBarHelper(this,layoutResID);
-        titleBarHelper.setBarTitle(getBarTitle());
-        titleBarHelper.getBackUpView().setOnClickListener(new BackUpListener());
-        setContentView(titleBarHelper.getContentView());
-    }
 
     /**
-     *设置标题名称
+     * 获取头部标题的TextView
+     * @return
      */
-    protected abstract String getBarTitle();
-
+    public TextView getToolbarTitle(){
+        return mToolbarTitle;
+    }
 
     /**
-     *返回监听
+     * 设置头部标题
+     * @param title
      */
-    protected  class BackUpListener implements View.OnClickListener{
-        @Override
-        public void onClick(View v) {
-            TitleBarActivity.this.finish();
+    public void setToolBarTitle(CharSequence title) {
+        if(mToolbarTitle != null){
+            mToolbarTitle.setText(title);
         }
     }
+
+    /**
+     * 是否显示后退按钮,默认显示,可在子类重写该方法.
+     * @return
+     */
+    protected boolean isShowBacking(){
+        return true;
+    }
+
+    protected abstract int getLayoutId();
+
+    protected void showToast(View view, String msg){
+        Snackbar.make(view, msg, Snackbar.LENGTH_LONG).show();
+    }
 }

+ 0 - 138
app/src/main/java/com/ynstkz/shitu/android/helper/TitleBarHelper.java

@@ -1,138 +0,0 @@
-package com.ynstkz.shitu.android.helper;
-
-import android.content.Context;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.FrameLayout;
-import android.widget.ImageView;
-import android.widget.RelativeLayout;
-import android.widget.TextView;
-
-import com.ynstkz.shitu.android.R;
-
-/**
- * 作者:fuchangle on 2018/1/27 22:28
- */
-
-public class TitleBarHelper {
-
-    /*上下文,创建view的时候需要用到*/
-    private Context mContext;
-
-    /*base view*/
-    private FrameLayout mContentView;
-
-    /*用户定义的view*/
-    private View mUserView;
-
-    /*视图构造器*/
-    private LayoutInflater mInflater;
-
-    private ImageView iv_back;
-    private TextView barTitle;
-    private TextView tv_title_right;
-    private ImageView ivTitleRight;
-    private RelativeLayout rlBarView;
-
-    public TitleBarHelper(Context context, int layoutId) {
-        this.mContext = context;
-        mInflater = LayoutInflater.from(mContext);
-        /*初始化整个内容*/
-        initContentView();
-        /*初始化用户定义的布局*/
-        initUserView(layoutId);
-        /*初始化titleBar*/
-        initTitleBar();
-    }
-
-
-    private void initContentView() {
-        /*直接创建一个帧布局,作为视图容器的父容器*/
-        mContentView = new FrameLayout(mContext);
-        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
-                ViewGroup.LayoutParams.MATCH_PARENT);
-        mContentView.setLayoutParams(params);
-    }
-
-    private void initTitleBar() {
-        /*通过inflater获取toolbar的布局文件*/
-        View titleBar = mInflater.inflate(R.layout.view_titlebar, mContentView);
-        iv_back = titleBar.findViewById(R.id.iv_back);
-        barTitle = titleBar.findViewById(R.id.tv_barTitle);
-        rlBarView = titleBar.findViewById(R.id.rl_bar_view);
-        tv_title_right = titleBar.findViewById(R.id.tv_title_right);
-        ivTitleRight = titleBar.findViewById(R.id.iv_title_right);
-    }
-
-    private void initUserView(int id) {
-        mUserView = mInflater.inflate(id, null);
-        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
-        params.topMargin = (int) (mContext.getResources().getDimension(R.dimen.titlebar_height)) - 1;
-        mContentView.addView(mUserView, params);
-    }
-
-    public FrameLayout getContentView() {
-        return mContentView;
-    }
-
-    /**
-     * 设置titlebar标题
-     * @param title
-     */
-    public void setBarTitle(String title){
-        barTitle.setText(title);
-    }
-
-    /**
-     * 获取返回按钮
-     * @return
-     */
-    public ImageView getBackUpView(){
-        return iv_back;
-    }
-
-    /**
-     * 右侧字体
-     * @return
-     */
-    public TextView getTitleRightTxt(){
-        return tv_title_right;
-    }
-
-    /**
-     * 右侧图片
-     * @return
-     */
-    public ImageView getIvTitleRight() {
-        return ivTitleRight;
-    }
-
-    public void setIvTitleRight(ImageView ivTitleRight) {
-        this.ivTitleRight = ivTitleRight;
-    }
-
-    public RelativeLayout getRlBarView() {
-        return rlBarView;
-    }
-
-    public void setRlBarView(RelativeLayout rlBarView) {
-        this.rlBarView = rlBarView;
-    }
-
-    public ImageView getIv_back() {
-        return iv_back;
-    }
-
-    public void setIv_back(ImageView iv_back) {
-        this.iv_back = iv_back;
-    }
-
-    public TextView getBarTitle() {
-        return barTitle;
-    }
-
-    public void setBarTitle(TextView barTitle) {
-        this.barTitle = barTitle;
-    }
-}

+ 669 - 0
app/src/main/java/com/ynstkz/shitu/android/utils/StatusBarUtil.java

@@ -0,0 +1,669 @@
+package com.ynstkz.shitu.android.utils;
+
+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.design.widget.CoordinatorLayout;
+import android.support.v4.widget.DrawerLayout;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.WindowManager;
+import android.widget.LinearLayout;
+
+import com.ynstkz.shitu.android.R;
+
+/**
+ * Created by Jaeger on 16/2/14.
+ * <p>
+ * 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);
+    }
+
+    /**
+     * 使状态栏半透明
+     * <p>
+     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
+     *
+     * @param activity 需要设置的activity
+     */
+    public static void setTranslucent(Activity activity) {
+        setTranslucent(activity, DEFAULT_STATUS_BAR_ALPHA);
+    }
+
+    /**
+     * 使状态栏半透明
+     * <p>
+     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
+     *
+     * @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, 使状态栏半透明
+     * <p>
+     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
+     *
+     * @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以上半透明效果,不建议使用)
+     * <p>
+     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
+     *
+     * @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.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;
+    }
+}

+ 6 - 2
app/src/main/res/layout/activity_home.xml

@@ -1,11 +1,15 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <include layout="@layout/view_title"/>
 
     <TextView
+        android:id="@+id/tv_barTitle"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:text="Hello World!" />
+        android:text="Hello World!"/>
 
 </LinearLayout>

+ 20 - 0
app/src/main/res/layout/view_title.xml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/view_title"
+    android:layout_width="match_parent"
+    android:layout_height="?attr/actionBarSize"
+    android:background="@color/colorPrimary">
+
+    <TextView
+        android:id="@+id/tv_title"
+        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_centerInParent="true"
+        android:lines="1"
+        android:ellipsize="end"
+        android:scrollHorizontally="true"
+        android:textColor="@color/white"/>
+
+</RelativeLayout>

+ 0 - 51
app/src/main/res/layout/view_titlebar.xml

@@ -1,51 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:orientation="vertical"
-    android:fitsSystemWindows="true"
-    android:background="#0000ff">
-
-    <RelativeLayout
-        android:id="@+id/rl_bar_view"
-        android:layout_width="match_parent"
-        android:layout_height="@dimen/titlebar_height">
-
-        <ImageView
-            android:id="@+id/iv_back"
-            android:layout_width="wrap_content"
-            android:layout_height="20dp"
-            android:layout_centerVertical="true"
-            android:layout_marginLeft="10dp"/>
-
-        <TextView
-            android:id="@+id/tv_barTitle"
-            android:layout_width="260dp"
-            android:layout_height="wrap_content"
-            android:layout_centerInParent="true"
-            android:gravity="center"
-            android:maxLines="1"
-            android:ellipsize="end"
-            android:textSize="18sp"/>
-
-        <TextView
-            android:id="@+id/tv_title_right"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:layout_alignParentRight="true"
-            android:layout_centerVertical="true"
-            android:padding="10dp"
-            android:textSize="14sp"
-            android:visibility="gone"/>
-
-        <ImageView
-            android:id="@+id/iv_title_right"
-            android:layout_width="20dp"
-            android:layout_height="20dp"
-            android:layout_alignParentRight="true"
-            android:layout_centerVertical="true"
-            android:layout_margin="10dp"
-            android:visibility="gone"/>
-    </RelativeLayout>
-
-</LinearLayout>

+ 0 - 9
app/src/main/res/values-v19/styles.xml

@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<resources>
-
-    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
-        <item name="android:windowTranslucentStatus">true</item>
-        <item name="android:windowTranslucentNavigation">true</item>
-    </style>
-
-</resources>

+ 0 - 12
app/src/main/res/values-v21/styles.xml

@@ -1,12 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<resources>
-
-    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
-        <item name="android:windowTranslucentStatus">false</item>
-        <item name="android:windowTranslucentNavigation">true</item>
-        <!--Android 5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色-->
-        <item name="android:statusBarColor">@android:color/transparent</item>
-
-    </style>
-
-</resources>

+ 2 - 0
app/src/main/res/values/colors.xml

@@ -4,5 +4,7 @@
     <color name="colorPrimaryDark">#303F9F</color>
     <color name="colorAccent">#FF4081</color>
 
+    <color name="black">#000000</color>
     <color name="white">#ffffff</color>
+    <color name="translucence">#0e000000</color>
 </resources>

+ 5 - 0
app/src/main/res/values/ids.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <item type="id" name="statusbarutil_fake_status_bar_view" />
+    <item type="id" name="statusbarutil_translucent_view" />
+</resources>

+ 1 - 1
library/build.gradle

@@ -21,4 +21,4 @@ android {
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.squareup.okhttp3:okhttp:3.3.1'
-}
+}