隨着安卓4.4的發佈,再也不用看到那些黑黑的狀態欄了,狀態欄也可以實現透明和自定義顏色了,學會狀態欄透明確實會讓你的APP好看不少。因爲誰能忍受那些大黑邊 。加上5.0md的發佈,靈活使用狀態欄配色可以表達各式各樣的主題心情,所以就是一句話,狀態欄配色雖然細小但是卻顯得十分重要了。
轉載自zhangphil大神的博客,轉載我就註明原文鏈接---http://blog.csdn.NET/zhangphil;
原本有三篇文章,我就融合了下當一篇文章發佈了。希望讀後能有所啓發吧。
老規矩,來句名言點綴下-------
——— 亞里士多德
一:用style樣式實現透明狀態欄
(1) 我的例子中,Androidmanifest.xml文件中定義的app的style爲AppTheme:
[html] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="zhangphil.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
上面的Androidmanifest.xml是Android Studio自動生成的,同時Android Studio自動在res/values目錄下生成的styles.xml文件中定義了AppTheme,我把這個AppTheme重新修改爲:
[html] view plain copy
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
</style>
</resources>
(2)再爲Android v19準備一套styles.xml文件。在res/目錄下新建一個名爲values-v19目錄,在res/values-v21目錄下再建一個styles.xml文件,注意名字和AppTheme相同:
[html] view plain copy
<resources>
<style name="AppTheme" parent="android:Theme.Holo.NoActionBar.TranslucentDecor">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
</resources>
(3)寫一個簡單的MainActivity.Java測試,MainActivity.Java代碼(特別注意!本例的MainActivity繼承自Activity而不是AppCompatActivity,如果繼承自AppCompatActivity,顯示結果達不到本例結果):
[java] view plain copy
package zhangphil.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
MainActivity.java加載的activity_main.xml代碼:
[html] view plain copy
<?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:background="@android:color/holo_orange_dark"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:text="zhang phil @ csdn" />
</LinearLayout>
代碼運行結果:
TextView跑到頂部狀態欄下面去了,這顯然不合適,在activity_main.xml代碼中增加android:fitsSystemWindows="true" :
[html] view plain copy
<?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:background="@android:color/holo_orange_dark"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:text="zhang phil @ csdn" />
</LinearLayout>
代碼運行結果:
顯示就正常了。
二:使用代碼實現狀態欄透明
附錄1以xml寫style實現了Android沉浸式(侵入式)狀態欄(標題欄),同樣以上層Java代碼實現。
在附錄文章1的基礎上,本例僅僅只是刪掉res目錄下的全部values-v21目錄所有資源文件,僅保留values下一個styles.xml文件定義的AppTheme:
[html] view plain copy
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light.NoActionBar">
</style>
</resources>
但是和附錄文章1比較,在MainActivity增加Java代碼:
[java] view plain copy
package zhangphil.myapplication;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// Translucent navigation bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
}
MainActivity需要的activity_main.xml和附錄文章1相同:
[html] view plain copy
<?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:background="@android:color/holo_orange_dark"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:text="zhang phil @ csdn" />
</LinearLayout>
代碼運行結果:
三,實現全部透明,在5.0以上會出現有底色覆蓋
從附錄文章1,2可以看到,依靠Android系統提供的標準方案,狀態欄即便在透明狀態下,仍然有些半透明而不是全透明。本文是Android沉浸式狀態欄解決方案的第三種,以Java代碼實現,參考附錄文章2,本文保持附錄文章2的styles.xml不變,僅僅只做上層Java代碼的調整,實現沉浸式狀態欄全透明或者動態設置顏色,測試的MainActivity.java:
[java] view plain copy
package zhangphil.myapplication;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//核心代碼.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//給狀態欄設置顏色。我設置透明。
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_main);
}
}
MainActivity.java需要的activity_main.xml文件:
[html] view plain copy
<?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:background="#e91e63"
android:clipToPadding="false"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:paddingTop="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:text="zhang phil @ csdn" />
</LinearLayout>
代碼運行結果:
注意:
1,本文的解決方案不在受限於MainActivity繼承自標準Activity,可以繼承自AppCompatActivity,但是如果MainActivity繼承自AppCompatActivity,那麼必須把styles.xml文件的style修改爲Theme.AppCompat.*****,否則代碼將崩潰,比如可以修改styles中的AppTheme:
[html] view plain copy
<resources>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
</style>
</resources>