編輯:Android 開發教學
結合 ToolBar 與自訂版面,實作沉浸式狀態欄的兩種方式。
沉浸式狀態欄嚴格來說應稱為透明狀態欄。一般情況下狀態欄底色為黑色,而沉浸式狀態欄則是把狀態欄設為透明或半透明。
這項特性從 Android KitKat(4.4)開始出現,可以設成與 App 頂部相同的顏色,讓切換 App 時整個畫面彷彿換成了同一套風格,視覺呈現也更為美觀。
本文說明結合 ToolBar 實現狀態欄的兩種做法,效果如下圖:

前提條件是 API 需大於等於 19(4.4 以上)。
方式一:
布局 toolbar1.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:app="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" app:title="App Title" app:subtitle="Sub Title" app:navigationIcon="@android:drawable/ic_input_add" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="沉浸式狀態欄" android:textSize="30sp" /> </RelativeLayout> </LinearLayout>
Activity.java
public class TooBarStatusActivity1 extends AppCompatActivity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//去標題 setContentView(R.layout.toolbar_layout); //透明狀態欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); //透明導航欄 Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitle("App Title"); //設置Toolbar標題 mToolbar.setSubtitle("Sub Title"); //設置Toolbar 副標題 mToolbar.setLogo(R.mipmap.ic_launcher);//設置Toolbar的Logo mToolbar.setNavigationIcon(R.mipmap.abc_ic_ab_back_mtrl_am_alpha); setSupportActionBar(mToolbar); } }
方式二:
布局 toolbar2.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="@color/colorAccent" android:fitsSystemWindows="true" android:clipToPadding="true" android:text="自定義的ToolBar布局" android:textSize="20sp" android:gravity="center_vertical" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFF00" android:text="主布局" android:textSize="22sp" android:gravity="center" /> </LinearLayout>
Activity.java
public class TooBarStatusActivity2 extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.toolbar_layout2); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導航欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } } }
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
Android 開發入門九:為視圖註冊事件監聽器
使用者與視圖互動時,Android 會觸發各種事件。常見做法包括為按鈕註冊點擊監聽器,或為輸入框註冊焦點變化監聽器。按鈕點擊監聽可透過 setOnClickListene
Android 滑動效果入門:ViewFlipper 輪播
ViewFlipper 是 Android 提供的 View 容器,可實現頁面切換與自動播放。特性繼承自 ViewAnimator,可放入多個子 View,並設定翻轉間隔
Android Studio 中 Gradle "xxx project refresh failed" 問題的解決方法
環境說明(2014.03.13 最新) 問題描述 問題:Gradle xxx project refresh failed 這是一個相當常見的問題。典型症狀是:之前明
Cocos2d-x 3.0 開發環境搭建:Python 與 setup.py 流程
Cocos2d-x 3.0 的環境建置比早期版本友善許多,但仍需先準備 Python 環境。官方工具多使用 Python 2.x,建議安裝後將 Python 路徑加入系統