Android設定狀態列透明與設定狀態列字型顏色Mode
阿新 • • 發佈:2018-12-30
#1 概念:
變色狀態列(透明狀態列):指的是狀態列與導航欄顏色相同,或者透明
沉浸式狀態列:,狀態列隱藏,在手指做了相關操作後,狀態列顯示出來,例如視訊播放器,在播放視訊時是隱藏狀態列的,但是點選螢幕的時候,狀態列會顯示出來,再例如文字閱讀器,在閱讀的時候是全屏的,然後從螢幕上方下滑或者下方上劃,虛擬鍵和狀態列出現了,但卻是直接覆蓋在程式文字上的,這是所謂的沉浸式狀態列。
#2 效果圖
(1)透明效果:
a. android 4.4以下不起作用(注意相容):
b. 4.4和5.0以上(5.0以上要呼叫另外的標記重新設定Flag,不然狀態列會有灰色的透明陰影效果)
(2)修改狀態列字型顏色 (6.0以下不起效果,不能直接設定,而是通過兩種模式,魅族、小米有對應暴露方法,但本文得考慮所有機型)
修改之後:
#3Demo地址: http://download.csdn.net/detail/baopengjian/9806226
#4 程式碼: (1)佈局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false" tools:context="transparentstatus.pbj.net.transparentstatus.MainActivity"> <View android:id="@+id/status_bar" android:layout_width="match_parent" android:layout_height="0dp" android:background="@android:color/holo_red_light" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/status_bar" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="根佈局為RelativeLayout時設定status_bar背景" /> <Button android:onClick="changeColor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改status_bar顏色" /> <Button android:onClick="goLinerLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳轉LinearLayout根佈局" /> <Button android:onClick="changeMode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_light" android:text="修改Mode" /> </LinearLayout> </RelativeLayout> (2)工具類
/**
* 設定狀態列透明工具類
* Created by baopengjian on 2017/4/7.
*/
public class UtilsStyle {
/**
* 佈局中必須有一個id為status_bar的view來設定狀態列背景
* 必須要在 setContentView之後呼叫
*
* @param activity 當前頁面
*/
public static void setTranslateStatusBar(Activity activity) {
// 4.4以上處理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // android
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);// 狀態列透明
View status_bar = activity.findViewById(R.id.status_bar);// 標題欄id
if (status_bar != null) {
ViewGroup.LayoutParams params = status_bar.getLayoutParams();
params.height = getStatusBarHeight(activity);
status_bar.setLayoutParams(params);
}
}
//5.0 以上處理
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}
private 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;
}
/**
* Flag只有在使用了FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
* 並且沒有使用 FLAG_TRANSLUCENT_STATUS的時候才有效,也就是隻有在狀態列全透明的時候才有效。
* @param activity
* @param bDark
*/
public static void setStatusBarMode(Activity activity, boolean bDark) {
//6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View decorView = activity.getWindow().getDecorView();
if (decorView != null) {
int vis = decorView.getSystemUiVisibility();
if (bDark) {
vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
} else {
vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(vis);
}
}
}
}
(3)使用
public class MainActivity extends AppCompatActivity {
private boolean isRed = true;
private boolean isDarkMode = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UtilsStyle.setTranslateStatusBar(this);
}
public void changeColor(View v){
View status_bar = findViewById(R.id.status_bar);
if(isRed){
status_bar.setBackgroundResource(android.R.color.holo_blue_light);
}else{
status_bar.setBackgroundResource(android.R.color.holo_red_light);
}
isRed = !isRed;
}
public void goLinerLayout(View v){
startActivity(new Intent(MainActivity.this,LinearRootActivity.class));
}
public void changeMode(View v){
UtilsStyle.setStatusBarMode(this,!isDarkMode);
isDarkMode = !isDarkMode;
}
}
修改之後:
#3Demo地址: http://download.csdn.net/detail/baopengjian/9806226
#4 程式碼: (1)佈局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="false" tools:context="transparentstatus.pbj.net.transparentstatus.MainActivity"> <View android:id="@+id/status_bar" android:layout_width="match_parent" android:layout_height="0dp" android:background="@android:color/holo_red_light" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/status_bar" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="根佈局為RelativeLayout時設定status_bar背景" /> <Button android:onClick="changeColor" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改status_bar顏色" /> <Button android:onClick="goLinerLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳轉LinearLayout根佈局" /> <Button android:onClick="changeMode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@android:color/holo_blue_light" android:text="修改Mode" /> </LinearLayout> </RelativeLayout> (2)工具類