android-状态栏沉浸式的解决方法

各种沉浸式状态栏的解决方法demol

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

以及上面这个例子的用法说明:https://blog.csdn.net/qiaoxiaoguang/article/details/79806987

以上的例子几乎可解决所有状态栏的问题,以下是彼人以前经常使用的沉浸状态栏方法,也可解决问题,但不全面,现在虽不用但也是一个知识点,在此记之。

备注:在写这个demol时粗心在基类的onCreate忘记调用抽象方法instantiation(),导致子类无法执行该方法,从而点击事件无响应,在此备注以免以后又犯同样的错

安卓系统应用情况:

目前安卓系统最高为8.0,但使用的很少。应用最广的系统是6.0,7.0的用户也在不断的增多。低版本的手机系统也会慢慢的被淘汰,低版本系统的手机很多app也安装不了,其实低于4.4系统的手机应该很少见了。所以适配系统一般不考虑低版本的。沉浸式状态栏在5.0(含)开始适配,低于该版本的状态栏还是系统默认的背景颜色

一、Activity状态栏沉浸式解决方法

      1.标题栏背景是色值,状态栏的处理方式。

      1)首先在values、values-v19、values-v21、values-v23文件夹下的styles.xml都设置一个ZaqAppThemeSingle的风格

扫描二维码关注公众号,回复: 6184142 查看本文章

        

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

3)在Activity的每个布局文件中的根布局加入“android:fitsSystemWindows=”true”,但是为了开发方便,可以在基类中添加以下红色框框代码来替换上面这句。

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

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

猜你喜欢

转载自blog.csdn.net/zaq977684/article/details/79712929