個(gè)人對前輩們所講的知識總結(jié)
Android 4.4中
默認(rèn)樣式
去掉ActionBar之后的樣式
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
狀態(tài)欄透明
在values-v19的主題屬性中添加一條即可,如下
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
導(dǎo)航欄透明
在values-v19的主題屬性中添加一條即可,如下
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowTranslucentNavigation">true</item>
</style>
調(diào)節(jié)布局位置
我們發(fā)現(xiàn)宁仔,狀態(tài)欄透明和導(dǎo)航欄透明都會出現(xiàn)布局內(nèi)容向上偏移的情況,而導(dǎo)航欄還會出現(xiàn) 內(nèi)容進(jìn)入導(dǎo)航區(qū)域的 情況膝但,如圖馋袜。
那怎么解決這一的情況呢茅糜?
其實(shí)我們要設(shè)置一個(gè)屬性,那就是“fitsSystemWindows”,fitsSystemWindows 是在 android 4.4 中引入的。
想了解詳細(xì)的fitsSystemWindows相關(guān)知識請去Google查閱下資料铝阐。
在主題屬性中添加‘a(chǎn)ndroid:fitsSystemWindows’,如下
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:fitsSystemWindows">true</item>
</style>
圖中的矩形區(qū)域就是TextView的區(qū)域铐拐,并沒有占用導(dǎo)航欄和狀態(tài)欄
其他方式實(shí)現(xiàn)
總結(jié)下上面的內(nèi)容:
- 狀態(tài)欄透明 在主題屬性中添加‘windowTranslucentStatus’
- 導(dǎo)航欄透明 在主題屬性中添加‘windowTranslucentNavigation’
- 調(diào)節(jié)布局位置 在主題屬性中添加‘fitsSystemWindows’
這種方式是在xml中設(shè)置屬性徘键,那么是否可以在代碼中設(shè)置屬性呢?同樣可以的遍蟋!
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//判斷SDK最小為19
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// windowTranslucentStatus
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// windowTranslucentNavigation
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
}
這樣的方式也是可以實(shí)現(xiàn)狀態(tài)欄和導(dǎo)航欄透明的吹害。但還是會出現(xiàn)布局偏移的情況,要怎么做呢虚青?
我們可以同樣像上面說的它呀,在主題屬性中進(jìn)行添加‘fitsSystemWindows’;我們還可以在相應(yīng)的布局文件中添加‘fitsSystemWindows’
如:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<!--其他控件-->
</RelativeLayout>
這樣設(shè)置也可以實(shí)現(xiàn)上面的效果棒厘。
需要注意的是纵穿,上面的代碼是在布局文件的根布局中添加的‘fitsSystemWindows’屬性,所以所以的子布局也會進(jìn)行相應(yīng)的偏移(其實(shí)偏移這個(gè)詞不夠準(zhǔn)確)奢人,也就是說 哪個(gè)控件添加了‘fitsSystemWindows’屬性谓媒,哪個(gè)控件就會進(jìn)行相應(yīng)的偏移,于是就會有這樣的效果
這個(gè)圖是5.0手機(jī)運(yùn)行的何乎,所以狀態(tài)欄沒有漸變
Android 5.0
由于要顯示整張圖片句惯,導(dǎo)致占的位置過大土辩,于是我將模擬器縮小了,導(dǎo)致圖片不清晰抢野,見諒哈拷淘。不影響我們看見效果
Android5.0狀態(tài)欄默認(rèn)顏色是獲取我們‘colorPrimaryDark’中的顏色,如果在主題中沒有添加‘colorPrimaryDark’屬性則會使用灰色做默認(rèn)顏色指孤。
在5.0中設(shè)置透明的方式和上面一樣启涯,我們按照上面方式設(shè)置,默認(rèn)情況下狀態(tài)欄和導(dǎo)航欄是灰色半透明的
狀態(tài)欄全透明
方法如下
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fitsSystemWindows">true</item>
<!--windowTranslucentStatus 默認(rèn)就是false,寫出來是怕你不知道 貼心吧↖(^ω^)↗-->
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--@android:color/transparent 是調(diào)用系統(tǒng)顏色:透明邓厕。-->
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
手賤的發(fā)現(xiàn)
既然頂部的狀態(tài)欄都可以全透明逝嚎,那底部的導(dǎo)航欄可不可以全透明呢扁瓢?于是手賤的開始了測試详恼。
我們想,既然有‘statusBarColor’這個(gè)屬性了引几,那么肯定是有‘navigationBarColor’這個(gè)屬性的了昧互。
statusBar全透明是這樣設(shè)置的:
windowTranslucentNavigation="true"
statusBarColor="透明色"
那么navigationBar全透明可不可以這樣設(shè)置:
windowTranslucentStatus="true"
navigationBarColor="透明色"
結(jié)果令我很失望,并沒有達(dá)到我想要的效果伟桅,全部變成了灰色~如圖:
那么是什么原因呢敞掘,我沒深入研究。
Android采用的是ARGB色彩模式楣铁,A代表Alpha玖雁,也就是透明度。而navigationBarColor和statusBarColor屬性設(shè)置值都是‘@android:color/transparent’盖腕,也就是 Color下的transparent~
等等赫冬!transparent是Color下的一個(gè)值!這也就是說我們可以設(shè)置自己的顏色嘍那么就試一下吧
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">#F0F</item>
<item name="android:navigationBarColor">#F0F</item>
看下效果圖溃列,為什么沒有是#F0F(紫色)而是半透明了呢劲厌?
仔細(xì)一看~‘windowTranslucentStatus’和‘windowTranslucentNavigation’都設(shè)置為true了,所以就還是原來的半透明了听隐。
于是我們可以得到一個(gè)小結(jié)論:‘windowTranslucentStatus’和‘windowTranslucentNavigation’設(shè)置為true后就再設(shè)置‘statusBarColor’和‘navigationBarColor’就沒有效果了。
修改下看看
<item name="android:statusBarColor">#F0F</item>
<item name="android:navigationBarColor">#F0F</item>
沒錯(cuò),我們修改成功了但是細(xì)心的朋友們發(fā)沒發(fā)現(xiàn)一個(gè)問題唇牧?沒發(fā)現(xiàn)的和上一個(gè)圖對比一下
是的肾档,我們并沒有設(shè)置‘fitsSystemWindows’!而紫色的那家伙自己下來了~仔細(xì)看開頭的那幾個(gè)‘11111’
這個(gè)時(shí)候你如果測試一下透明度你就會發(fā)現(xiàn)沪么,狀態(tài)欄和導(dǎo)航欄的底色其實(shí)是灰色
修改下看看
<item name="android:statusBarColor">#1F0F</item>
<item name="android:navigationBarColor">#1F0F</item>
我們看見乌企,幾乎就是灰色了。
具體什么原因成玫,我也沒有深入研究加酵。
但是呢拳喻,其實(shí)工作中很少會將導(dǎo)航欄調(diào)色的,一般都是半透明的猪腕。于是乎就會有下面的配置
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">#8F0F</item>
上面說的都是在xml中設(shè)置陋葡,那么在代碼中怎么設(shè)置呢亚亲?
getWindow().setNavigationBarColor(int i);
getWindow().setStatusBarColor(int i);
當(dāng)然了,在項(xiàng)目中使用還是要判斷下版本噠
結(jié)束
上面兩個(gè)問題我說我沒用深入研究腐缤,等有機(jī)會捌归、有時(shí)間在弄吧,暫時(shí)就總結(jié)這么多吧岭粤,這玩意我也是剛用到惜索,還有很多東西等我們探索呢。
GitHub有個(gè)項(xiàng)目不錯(cuò),有時(shí)間可以看看源碼
地址:https://github.com/laobie/StatusBarUtil
我也是參考文章學(xué)習(xí)的剃浇,推薦幾個(gè)不錯(cuò)的文章吧:
http://www.reibang.com/p/0acc12c29c1b
http://blog.csdn.net/lmj623565791/article/details/48649563
http://jaeger.itscoder.com/android/2016/02/15/status-bar-demo.html