1. 程式人生 > >Android 導航欄ActionBar和狀態列StautsBar的定製

Android 導航欄ActionBar和狀態列StautsBar的定製

有關Theme主題

<resources>

    <!-- 所有主題可以新增 .NoActionBar 讓其變成無ActionBar的Activity-->

    <!-- 所有使用AppCompat相容主題前提條件如下:
        1.當前Activity繼承AppCompatActivity
        2.新增'com.android.support:appcompat-v7:23.0.1'支援包
        3.在onCreate方法中使用getSupportActionBar()方法獲得當前Activity的ActionBar

        註釋:此主題理論上相容所有API
    -->
<!-- 主題亮色,actionbar暗色. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> <!-- 主題亮色,actionbar亮色. --> <style name="AppTheme2" parent="Theme.AppCompat.Light"> <!--
Customize your theme here. -->
</style> <!-- 預設情況,所有暗色. --> <style name="AppTheme3" parent="Theme.AppCompat"> <!-- Customize your theme here. --> </style> <!-- =======================================================--> <!-- =======================================================-->
<!-- 所有使用Material主題的前提條件如下: 1.API>=21 使用才有意義 --> <!-- 主題亮色,actionbar暗色. --> <style name="AppTheme4" parent="android:style/Theme.Material.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> <!-- 主題亮色,actionbar亮色. --> <style name="AppTheme5" parent="android:style/Theme.Material.Light"> <!-- 自定義ActionBar的風格--> <item name="android:actionBarStyle">@style/AppTheme5.ActionBar</item> </style> <!-- 自定義的ActionBar風格--> <style name="AppTheme5.ActionBar" parent="android:style/Theme.Material.Light"> <!-- actionBar背景--> <item name="android:background">@android:color/white</item> <!-- actionBar陰影高度,需要API>=21才有效--> <item name="android:elevation">@dimen/activity_horizontal_margin</item> <!-- 顯示返回鍵和標題--> <item name="android:displayOptions">homeAsUp|showTitle</item> </style> <!-- 預設情況,所有暗色. --> <style name="AppTheme6" parent="android:style/Theme.Material"> <!-- Customize your theme here. --> </style> <!--這個主題和上一個主題的區別就是有一個“@”字首,如果有“@”字首的表示只支援API>=21--> <style name="AppTheme7" parent="@android:style/Theme.Material"> <!-- Customize your theme here. --> </style> <!-- =======================================================--> <!-- =======================================================--> <!--Holo主題前提條件如下: 1.API >=11 --> <!-- 主題亮色,actionbar暗色. --> <style name="AppTheme8" parent="android:Theme.Holo.Light.DarkActionBar"> <!-- Customize your theme here. --> </style> <!-- 主題亮色,actionbar亮色. --> <style name="AppTheme9" parent="android:Theme.Holo.Light"> <!-- Customize your theme here. --> </style> <!-- 預設情況,所有暗色. --> <style name="AppThem10" parent="android:Theme.Holo"> <!-- Customize your theme here. --> </style> </resources>

無ActionBar透明statusBar

Android4.4以上直接引用如下主題即可

Theme.Holo.Light.NoActionBar.TranslucentDecor

android:Theme.Material.Light.NoActionBar.TranslucentDecor

這裡寫圖片描述

有ActionBar透明statusBar

Android4.4以上新增如下主題屬性即可

<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>

這裡寫圖片描述

此時StatusBar的背景顏色和當前視窗的背景顏色一樣,因此,我們可以通過修改視窗背景顏色來間接達到修改狀態列StatusBar的背景顏色。

使用ToolBar來定製ActionBar以及狀態列StatusBar的背景

使用者控制顯示隱藏ActionBar

主要用於當ActionBar顯示幾秒之後會自動隱藏,當再次點選螢幕時ActionBar又會顯示。

1.在主題中新增如下屬性使得當前ActionBar可以懸浮在佈局之上

<item name="android:windowActionBarOverlay">true</item>

2.在程式碼中控制多少秒之後ActionBar隱藏,以及點選螢幕ActionBar再次顯示出來。

public class MainActivity extends Activity {

    private ActionBar mActionBar;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActionBar = getActionBar();
        if (null != mActionBar) {
            hideActionBar();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (null != mActionBar) {
                hideActionBar();
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private void hideActionBar() {
        mActionBar.show();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mActionBar.isShowing()) {
                    mActionBar.hide();
                }
            }
        }, 3000);
    }

}