曾使用过一次设置透明状态栏的开源库,地址:https://github.com/jgilfelt/SystemBarTint 部分手机未成功。在这记录搜寻到的另一份使用方式,目前手上的手机均通过。但是否完全可以用,还待有其他手机再测试。先记录代码如下:
- public void setStateBarColor(Activity activity) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
- //5.0 全透明实现
- //getWindow.setStatusBarColor(Color.TRANSPARENT)
- 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);
- } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- //4.4 全透明状态栏
- activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
- }
- // 设置状态栏颜色
- ViewGroup contentLayout = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
- SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
- SystemBarTintManager.SystemBarConfig config = systemBarTintManager.getConfig();
- int actionBarHeight = config.getActionBarHeight();
- contentLayout.getChildAt(0).setPadding(0, getStatusBarHeight(activity) + actionBarHeight, 0, 0);
- if (mStatusBarColor == 0) {
- setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));
- } else {
- setupStatusBarView(activity, contentLayout, mStatusBarColor);
- }
- // 设置Activity layout的fitsSystemWindows
- View contentChild = contentLayout.getChildAt(0);
- contentChild.setFitsSystemWindows(true);//等同于在根布局设置android:fitsSystemWindows="true"
- }
public void setStateBarColor(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.0 全透明实现
//getWindow.setStatusBarColor(Color.TRANSPARENT)
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);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//4.4 全透明状态栏
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
// 设置状态栏颜色
ViewGroup contentLayout = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);
SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
SystemBarTintManager.SystemBarConfig config = systemBarTintManager.getConfig();
int actionBarHeight = config.getActionBarHeight();
contentLayout.getChildAt(0).setPadding(0, getStatusBarHeight(activity) + actionBarHeight, 0, 0);
if (mStatusBarColor == 0) {
setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));
} else {
setupStatusBarView(activity, contentLayout, mStatusBarColor);
}
// 设置Activity layout的fitsSystemWindows
View contentChild = contentLayout.getChildAt(0);
contentChild.setFitsSystemWindows(true);//等同于在根布局设置android:fitsSystemWindows="true"
}