先貼個(gè)博客吧 ?大神們的東西畢竟666
http://blog.csdn.net/lmj623565791/article/details/48649563;
下面說(shuō)說(shuō)我自己的解決辦法
看了N篇博客,大概改變狀態(tài)欄背景色的api是從API19才開(kāi)始的,最早支持4.4貌似
5.0以后就可以很炫酷的換膚了。不過(guò)想改的話貌似一大堆代碼,我就偷了個(gè)懶,用最笨的方式改變了,僅供參考
下面給出我的代碼
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
//透明狀態(tài)欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明導(dǎo)航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
這是onCreate里面的代碼,為了方便看出代碼放置的位置,上下各沾了標(biāo)明位置的代碼。方法比較常規(guī)判斷版本
然后根據(jù)版本對(duì)狀態(tài)欄進(jìn)行處理
再看看我的布局文件夾
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
?<RelativeLayout
android:id="@+id/status_bar_view"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:background="@color/colorAccent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/status_bar_view"
android:background="@color/colorAccent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:text="Back"
android:textColor="@color/white"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="Title"
android:textColor="@color/white"
android:textSize="18sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="10dp"
android:text="Sure"
android:textColor="@color/white"
android:textSize="18sp"/>
? </RelativeLayout>
</RelativeLayout>
依舊是中規(guī)中矩的布局,效果圖如下

這里我的title包含兩部分內(nèi)容,上面純色的部分是為了防止設(shè)置狀態(tài)欄透明后布局頂上去被遮擋預(yù)留的,如果想看到差異在哪里可以將上面的RelativeLayout設(shè)置隱藏看下效果,而且真正處理背景色的部分也就是這里,下面給出處理代碼
setContentView(R.layout.activity_main);
RelativeLayoutstatusBars=(RelativeLayout)findViewById(R.id.status_bar_view);
//如果是API19及以上的版本 這樣處理 否則就要隱藏了
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
statusBars.setBackgroundResource(R.color.blue);
}else{
statusBars.setVisibility(View.GONE);
}
沒(méi)了,就這么多,你要處理的代碼其實(shí)就一行,設(shè)置跟你的title一樣的顏色就可以無(wú)縫拼接了,這里我設(shè)置替代狀態(tài)欄布局的高度是25dp,是不是別的機(jī)型也是這個(gè)高度,沒(méi)做過(guò)詳細(xì)的研究。是不是很輕量?根本么有什么第三方包,也沒(méi)有一大堆的邏輯處理,驚不驚喜?意不意外?
條條大道去羅馬,實(shí)現(xiàn)你想要的效果才是最終目的,個(gè)人愚見(jiàn),歡迎討論(PS:本方法適用于改變狀態(tài)欄背景色,不適用于改變狀態(tài)欄字體色)