摘要 Android 沉浸式状态栏、状态栏一体化、透明状态栏、仿ios透明状态栏
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"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:fitsSystemWindows="true" android:clipToPadding="true" android:layout_width="match_parent" android:layout_height="100dp" android:background="#009959" android:text="你好,请问你有男朋友吗"/> <Button android:layout_width="100dp" android:layout_height="50dp" android:background="#ff669d"/> </LinearLayout>
@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; }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/app_bg" > <!--其他布局--> <RelativeLayout android:id="@+id/index_title_bar" android:layout_alignParentTop="true" style="@style/title_bar_style_home" android:fitsSystemWindows="true" android:gravity="center" > <TextView android:id="@+id/current_city_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:drawableTop="@drawable/ic_location" android:drawablePadding="2dp" android:text="北京" android:textColor="#fff" android:textSize="10sp" /> <TextView android:id="@+id/tv_title" android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="33dp" android:gravity="center_vertical" android:layout_weight="10" android:background="@drawable/shape_edit_corners_bg" android:hint="请输入商品名称" android:imeOptions="actionSearch" android:singleLine="true" android:textColor="@color/black_text" android:textColorHint="#ffb6b6b6" android:textSize="14sp" android:maxLength="10" android:paddingLeft="5dp" android:paddingRight="3dp" android:drawableLeft="@drawable/ic_search" android:drawableRight="@drawable/bg_btn_voice" android:layout_toLeftOf="@+id/image_right" android:layout_toRightOf="@+id/current_city_text" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"/> <TextView android:id="@+id/image_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/ic_category" android:drawablePadding="2dp" android:text="分类" android:textColor="#fff" android:textSize="10sp" /> </RelativeLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/app_bg" android:orientation="vertical" > <!-- 其他布局 --> <RelativeLayout android:id="@+id/index_title_bar" android:layout_alignParentTop="true" style="@style/title_bar_style_home_v19" android:fitsSystemWindows="true" android:gravity="center" android:paddingTop="@dimen/title_bar_padding_top" > <TextView android:id="@+id/current_city_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:drawableTop="@drawable/ic_location" android:drawablePadding="2dp" android:text="北京" android:textColor="#fff" android:textSize="10sp" /> <TextView android:id="@+id/tv_title" android:layout_centerVertical="true" android:layout_width="match_parent" android:layout_height="33dp" android:gravity="center_vertical" android:layout_weight="10" android:background="@drawable/shape_edit_corners_bg" android:hint="请输入商品名称" android:imeOptions="actionSearch" android:singleLine="true" android:textColor="@color/black_text" android:textColorHint="#ffb6b6b6" android:textSize="14sp" android:maxLength="10" android:paddingLeft="5dp" android:paddingRight="3dp" android:drawableLeft="@drawable/ic_search" android:drawableRight="@drawable/bg_btn_voice" android:layout_toLeftOf="@+id/image_right" android:layout_toRightOf="@+id/current_city_text" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"/> <TextView android:id="@+id/image_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:drawableTop="@drawable/ic_category" android:drawablePadding="2dp" android:text="分类" android:textColor="#fff" android:textSize="10sp" /> </RelativeLayout> </RelativeLayout>
<style name="title_bar_style_home"><!-- 首页用 --> <item name="android:background">#ffc6003b</item> <item name="android:paddingLeft">0.0dip</item> <item name="android:paddingRight">0.0dip</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">48dp</item> </style> <style name="title_bar_style_home_v19"><!-- 首页用v19 --> <item name="android:background">#ffc6003b</item> <item name="android:paddingLeft">0.0dip</item> <item name="android:paddingRight">0.0dip</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">65dp</item> </style>
<dimen name="title_bar_padding_top">0dp</dimen>values-v19/dimens.xml
<dimen name="title_bar_padding_top">20dp</dimen>
@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; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/title_bar_main" /> <!-- 其他布局 --> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_title_bar" style="@style/title_bar_style_main" android:fitsSystemWindows="true" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFEFEFE"> <TextView android:id="@+id/title_text" style="@style/title_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:ellipsize="end" android:singleLine="true" /> <ImageView android:id="@+id/right_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@null" android:paddingLeft="15dp" android:paddingRight="15dp" android:src="@drawable/ic_notice" android:visibility="gone"/> </RelativeLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/root_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/title_bar_main_v19" /> <!-- 其他布局 --> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_title_bar" style="@style/title_bar_style_main_19" android:fitsSystemWindows="true" android:paddingTop="@dimen/title_bar_padding_top"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFEFEFE"> <TextView android:id="@+id/title_text" style="@style/title_text_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:ellipsize="end" android:singleLine="true" /> <ImageView android:id="@+id/right_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:contentDescription="@null" android:paddingLeft="15dp" android:paddingRight="15dp" android:src="@drawable/ic_notice" android:visibility="gone"/> </RelativeLayout> </LinearLayout>
<style name="title_bar_style_main"> <item name="android:background">#FFFEFEFE</item> <item name="android:paddingLeft">0.0dip</item> <item name="android:paddingRight">0.0dip</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">48dp</item> <item name="android:gravity">center_vertical</item> </style> <style name="title_bar_style_main_19"> <item name="android:background">@android:color/black</item> <item name="android:paddingLeft">0.0dip</item> <item name="android:paddingRight">0.0dip</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">68dp</item> <item name="android:gravity">center_vertical</item> </style>