導讀:
最近在使用自己手機,發現一種動畫效果非常不錯,就是天氣隨着時間季節,狀態欄和ToorBar顏色都會發生改變,搜索資料得出的三種方案….
方案一
將手機狀態欄透明化: 狀態欄透明瞭,狀態欄的顏色就會自動跟隨下面的佈局
兩種方式:
1.代碼方式
/**
* 設置透明狀態欄
* <p>
* 可在Activity的onCreat()中調用
* <p>
* 注意:需在頂部控件佈局中加入以下屬性讓內容出現在狀態欄之下:
* android:clipToPadding="true" // true 會貼近上層佈局 ; false 與上層佈局有一定間隙
* android:fitsSystemWindows="true" //true 會保留actionBar,title,虛擬鍵的空間 ; false 不保留
*
* @param activity activity
*/
public static void setTransparentStatusBar(Activity activity) {
//5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = activity.getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
//4.4到5.0
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
}
}
---------------------分割線--------------------------
2.在values,values-v19,values-v21目錄下分別 創建主題
//values
<style name="TranslucentTheme" parent="AppTheme">
</style>
//values-v19
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
//values-v21
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
效果圖:
方案二 (推薦)
嵌套佈局,修改佈局文件的根佈局爲狀態欄着色,子步局用來指定頁面的背景顏色
1.如方案一將狀態欄透明化
2.修改佈局文件
//善用 這兩個屬性
//android:clipToPadding="true" // true 會貼近上層佈局 ; false 與上層佈局有一定間隙
//android:fitsSystemWindows="true" //true 會保留actionBar,title,虛擬鍵的空間 ; false 不保留
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff9900"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00000"
android:theme="@style/AppTheme.AppBarOverlay"
app:title="Text" />
</LinearLayout>
</RelativeLayout>
效果圖:
方案三
使用android:fitsSystemWindows=”true”屬性,給狀態欄留出空間,添加一個和狀態欄寬高相同的View覆蓋透明的狀態欄
一.如方案一將狀態欄透明化
二.指定狀態欄高度,兩種方式:
1.代碼獲取狀態欄高度:
/**
* 獲取狀態欄高度
*
* @param context 上下文
* @return 狀態欄高度
*/
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
-------------------------分割線------------------------------
2.在values,valuse-v19 目錄下直接指定高度:
(這種方式可能會因爲設備分辨率不同,有稍微誤差)
//values
<dimen name="padding_top">0dp</dimen>
//values-v19
<dimen name="padding_top">25dp</dimen>
3.添加一個View覆蓋狀態欄
private void addStatusBarView() {
View view = new View(this);
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(this));
ViewGroup decorView = (ViewGroup) findViewById(android.R.id.content);
decorView.addView(view, params);
}
效果圖:
封裝的一個工具類:
注意使用上面強調的兩個屬性在佈局中的使用
public class StatusBar {
/**
* 設置透明狀態欄
* <p>
* 可在Activity的onCreat()中調用
* <p>
* 注意:需在頂部控件佈局中加入以下屬性讓內容出現在狀態欄之下:
* android:clipToPadding="true" // true 會貼近上層佈局 ; false 與上層佈局有一定間隙
* android:fitsSystemWindows="true" //true 會保留actionBar,title,虛擬鍵的空間 ; false 不保留
*
* @param activity activity
*/
public static void setTransparentStatusBar(Activity activity) {
//5.0及以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
View decorView = activity.getWindow().getDecorView();
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
/**
* 如果上面無效,用這個
*activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
*activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
*/
//4.4到5.0
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WindowManager.LayoutParams localLayoutParams = activity.getWindow().getAttributes();
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
/**
* 如果上面無效,用這個
* activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
*/
}
}
/**
* 獲取狀態欄高度
*
* @param context 上下文
* @return 狀態欄高度
*/
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
/**
* 判斷狀態欄是否存在
*
* @param activity activity
* @return true :存在 ; false: 不存在
*/
public static boolean isStatusBarExists(Activity activity) {
WindowManager.LayoutParams params = activity.getWindow().getAttributes();
return (params.flags & WindowManager.LayoutParams.FLAG_FULLSCREEN) != WindowManager.LayoutParams
.FLAG_FULLSCREEN;
}
/**
* 添加狀態欄View
*
* @param activity 需要設置的 activity
* @param argb Color.argb(alpha, 0, 0, 0) 顏色屬性
*/
private static void addStatusBarView(Activity activity, int argb) {
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
// 移除半透明矩形,以免疊加
if (contentView.getChildCount() > 1) {
contentView.removeViewAt(1);
}
contentView.addView(createStatusBarView(activity, argb));
}
/**
* 創建矩形 View
*
* @param argb Color.argb(alpha, 0, 0, 0) 顏色屬性
* @return View
*/
private static View createStatusBarView(Activity activity, int argb) {
// 繪製一個和狀態欄一樣高的矩形
View statusBarView = new View(activity);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(argb);
return statusBarView;
}
}