天天看點

android-狀态欄沉浸式的解決方法

各種沉浸式狀态欄的解決方法demol

https://download.csdn.net/download/zaq977684/10687660

以及上面這個例子的用法說明:https://blog.csdn.net/qiaoxiaoguang/article/details/79806987

以上的例子幾乎可解決所有狀态欄的問題,以下是彼人以前經常使用的沉浸狀态欄方法,也可解決問題,但不全面,現在雖不用但也是一個知識點,在此記之。

備注:在寫這個demol時粗心在基類的onCreate忘記調用抽象方法instantiation(),導緻子類無法執行該方法,進而點選事件無響應,在此備注以免以後又犯同樣的錯

android-狀态欄沉浸式的解決方法

安卓系統應用情況:

目前安卓系統最高為8.0,但使用的很少。應用最廣的系統是6.0,7.0的使用者也在不斷的增多。低版本的手機系統也會慢慢的被淘汰,低版本系統的手機很多app也安裝不了,其實低于4.4系統的手機應該很少見了。是以适配系統一般不考慮低版本的。沉浸式狀态欄在5.0(含)開始适配,低于該版本的狀态欄還是系統預設的背景顔色

一、Activity狀态欄沉浸式解決方法

      1.标題欄背景是色值,狀态欄的處理方式。

      1)首先在values、values-v19、values-v21、values-v23檔案夾下的styles.xml都設定一個ZaqAppThemeSingle的風格

android-狀态欄沉浸式的解決方法

values:

<style name="ZaqAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@android:color/white</item>
    <item name="colorPrimaryDark">@android:color/white</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:windowAnimationStyle">@style/windows_define_animation</item><!--界面跳轉的動畫-->

</style>
           

values-v19:

<style name="ZaqAppThemeSingle" parent="ZaqAppTheme">
    <!-- Customize your theme here. -->
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
</style>
           

values-v21:

<style name="ZaqAppThemeSingle" parent="ZaqAppTheme">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
           

values-v23:

<style name="ZaqAppThemeSingle" parent="ZaqAppTheme">
    <!--跟代碼控制一緻-->
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <!--設定狀态欄-->
    <item name="android:windowTranslucentStatus">false</item>
    <!--手機底部的虛拟化顔色-->
    <item name="android:windowTranslucentNavigation">false</item>
    <!--Android 5.x開始需要把顔色設定透明,否則導航欄會呈現系統預設的淺灰色-->
    <item name="android:statusBarColor">@android:color/transparent</item>
    <!--該屬性為android6.0版本之後的新功能,改變狀态欄本身系統顯示的顔色值 -->
    <item name="android:windowLightStatusBar">true</item>
</style>
           

2) 在清單檔案中配置需要沉浸式狀态欄的activity加入theme

android-狀态欄沉浸式的解決方法

3)在Activity的每個布局檔案中的根布局加入“android:fitsSystemWindows=”true”,但是為了開發友善,可以在基類中添加以下紅色框框代碼來替換上面這句。

android-狀态欄沉浸式的解決方法

2.标題欄背景是圖檔,狀态欄的處理方式。

 1)首先在values、values-v19、values-v21檔案夾下的styles.xml都設定一個ZaqImgThemeSingle風格

values:

<style name="ZaqImgTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowAnimationStyle">@style/windows_define_animation</item><!--界面跳轉的動畫-->
</style>
           
values19:
           
<!--圖檔作為狀态欄的背景色使用的theme-->
    <style name="ZaqImgThemeSingle" parent="ZaqImgTheme">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>
           

values21:

<style name="ZaqImgThemeSingle" parent="ZaqImgTheme">
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>
    <!--Android 5.x開始需要把顔色設定透明,否則導航欄會呈現系統預設的淺灰色-->
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
           

2)在清單檔案中配置需要沉浸式圖檔狀态欄的activity加入theme

android-狀态欄沉浸式的解決方法

3)同(1.标題欄背景是色值,狀态欄的處理方式。)步驟3

二、fragment沉浸式狀态欄,一個Activity嵌套多個frgment,所有的fragment的标題欄色值都一樣

1)在Activity中關聯布局後設定狀态欄色值

public  void setStatusBarColor(int color) {
    // 5.0 以上
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(color);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        View view = new View(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
        view.setLayoutParams(params);
        view.setBackgroundColor(color);
        ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
        decorView.addView(view);
    }
}
           
/**
 * 擷取狀态欄的高度
 */
public  int getStatusBarHeight() {
    int statusBarHeightId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    return getResources().getDimensionPixelOffset(statusBarHeightId);
}
           

2)在清單檔案AndroidManifest.xml中把該Activity的theme設定成ZaqNoTitleTheme

在values檔案:
           
<style name="ZaqNoTitleTheme" parent="Theme.AppCompat.Light.NoActionBar">
           
<item name="android:windowBackground">@color/black_mainme</item></style>
           
3)同(1.标題欄背景是色值,狀态欄的處理方式。)步驟3
           
例子下載下傳連結
           

https://download.csdn.net/download/zaq977684/10339071