摘要 Android 沉浸式状态栏、状态栏一体化、透明状态栏、仿ios透明状态栏
推荐好用的工具类:http://jaeger.itscoder.com/android/2016/03/27/statusbar-util.html
git地址:https://github.com/laobie/StatusBarUtil
Google 在 4.4 给全屏阅读文字或玩游戏这种情景增加了透明状态栏和透明导航栏的功能。
首先上两张乐家驿站应用的截图,实际效果大家也可以下载体验,乐家驿站下载:http://www.wandoujia.com/apps/com.haier.cabinet.customer
注意:本文是自定义标题栏,不用android默认自带的标题栏。
第一步:设置 Acitivity 所在 window 的属性
@Override
public 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);
}
}
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
只要加入上面两行代码,就可以实现沉浸式通知栏了。效果如图:
android:fitsSystemWindows="true"
android:clipToPadding="true"
@Override
public 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);
}
// 强制更新
UmengUpdateAgent.setDialogListener(new UmengDialogButtonListener() {
@Override
public void onClick(int status) {
switch (status) {
case UpdateStatus.Update:
break;
default:
// close the app
AppToast.showShortText(MainUIActivity.this,
"非常抱歉,您需要更新应用才能继续使用");
MainUIActivity.this.finish();
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view = inflater.inflate(R.layout.fragment_home_v19, container, false);
}
return view;
}
name="title_bar_padding_top">0dp
values-v19/dimens.xml
name="title_bar_padding_top">20dp
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_life, container, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
view = inflater.inflate(R.layout.fragment_life_v19, container, false);
}
return view;
}