轉載自大力哥博客
最近負責的幾個項目都被要求將系統狀態欄的顏色設置與app主題色一致,網上找到許多資料都沒有一下解決我的問題,最終結合幾個解決方案總結出一個最簡單、最快的狀態欄一體化方案,只要三步你的app就將有更好的用戶體驗了。
注意:由于Google只在Android4.4之后可以修改狀態欄顏色,所以本方案只有在4.4以上才有效果(PS:國內大部分手機都已經使用4.4以上系統,5.0、5.1、6.0等也在逐漸增加,特別是Android6.0 是目前新出的手機的標配,4.4以下都可以不用考慮了)
先來看看使用前后的效果對比吧:(第一張是使用前,第二張是使用后)
不多說,只要三步即可:
1、拷貝狀態欄管理類SystemStatusManager到你的項目中(下載地址在最后);
2、在你的BaseActivity中的onCreate方法中調用一下代碼(如果只要某個頁面一體化,可以在需要一體化的頁面加入改語句):
new SystemStatusManager(this).setTranslucentStatus(R.color.colorPRimary);//設置狀態欄透明,參數為你要設置的顏色1注意:如果狀態欄與標題欄中間有一條線導致有過渡感,那么在你項目的主題(style文件)中加入去掉線條的語句即可:
<item name="android:windowContentOverlay">@null</item>11、在BaseActivity中使用:
/** * Created by HDL on 2016/7/22. */public class BaseActivity extends Activity { public Activity mActivity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActivity = this; new SystemStatusManager(this).setTranslucentStatus(R.color.colorPrimary);//設置狀態欄透明 }}123456789101112132、根布局加入屬性
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:orientation="vertical" tools:context="com.hdl.newkjdemo.newkjdemo.ui.main.MainActivity"> <include layout="@layout/custom_actionbar" />12345678910這里是下載地址
新聞熱點
疑難解答