不完美总结android4.4+透明状态栏

android4.4以上出现防ios的状态栏颜色修改,到android5.0就完全适用,使用简单,所以我们很需要考虑4.4的情况,api为19 ***(应该知道);纠正说法:是透明状态栏,不是沉浸式状态栏,所谓的沉浸式状态栏是全屏幕都是画面,一般游戏和小说状态会使用到。那么下面开车:

1:android 5.0+
android5.0使用就很简单了,我们只需要设计app的Theme的属性就可以了,一般都是采用主题为:NoActionBar;

在默认的values文件夹下的styles主题设置,其中colorPrimaryDark就是设置状态栏的颜色,这种情况我们就可以在布局里面使用toolbar来展示布局了:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.js.shubin.zhuangtailanapplication.MainActivity">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:visibility="gone"
        android:src="@mipmap/shanshui"/>
    
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:visibility="visible"
        android:background="?attr/colorPrimary"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/meijing"
         />
</LinearLayout>
不要问为什么使用toolbar,设置后我们运行看结果图

效果出来了。。好简单。。。。。那么不是5.0以上呢?莫急莫急,哥哥会 记下来

2:android4.4+ api19

android 5.0之前是要自己设置代码才能显示透明栏,当然我这次想说的就是不用代码只用布局。4.4的情况下,我们先创建一个values的资源文件文件下个styles-v19的xml文件


设置主题和正常的styles一样不过要加一个<item name="android:windowIsTranslucent">true</item>:

哎呀,好像失败了,哈哈哈咯。。。。

那么怎么办呢,。。。其实很简单,你只要再加一个就能成功的实现了:

没错就是 <item name="android:windowTranslucentNavigation">true</item>,如果你上一步已经成功了就不要加了,我们看看效果:

成功了。。。。。。。但是是不是发现不对劲?整个都爬上去了??这个我们就要加另一个属性了,

android:paddingTop="@dimen/toolbar_padding_top"

他怎么这么强?只要在v19里面设置<dimen name="toolbar_padding_top">25dp</dimen>就ok了(同时,其他style的就设置为0,),恩,状态栏距离顶部25dp...???然后我们再看看

.。。。。。成功了。。。

其实我们可能不用ToolBar,自己写一个公共的标题栏然后inculde,向你们这种不思进取的人,我只想说,我也是这样的。。。。于是我直接也来看一下,自己写一个标题栏,不用toolbar,5.0以上是没问题的,还是那么简单,4.4呢,结果是这样的。。。先看布局吧
 <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:visibility="visible"
        android:background="@color/colorAccent"
        android:layout_height="45dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="17sp"
            android:text="标题"/>
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/meijing"
         />
运行结果是这样的;;;
哦,肯定是没有加android:paddingTop="@dimen/toolbar_padding_top",于是我聪明的在里面加上了.真是机智如我,
我勒个去。。。。。。

哈哈哈,我陷入了沉思。。。这个时候就要用这个大兄弟了。。。android:fitsSystemWindows="true",怎么用呢??很简单

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:fitsSystemWindows="true">

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:visibility="visible"
        android:background="@color/colorAccent"
        android:paddingTop="@dimen/toolbar_padding_top"
        android:layout_height="45dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="17sp"
            android:text="标题"/>
    </LinearLayout>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/meijing"
         />
我们加一个空的LinearLayout,看上面,看上面,看上面。。然后设置android:fitsSystemWindows="true",然后我们看结果。。。

成功了。。。。(当然,5.0上没有影响)


3、图片的透明状态栏

你已经注意到了最上面的布局,有一个ImageView,设置为gone,这里有用咯。。。其实图片的透明状态栏,让图片渗入到状态栏的做法。。就是将手机自己的状态栏去掉,那么图片就上去了。。。。。。嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎。。

那么在4.4的情况下,我们上面的操作不改动,直接设置图片。。。

成功了。。。。。。。。。。

那么5.0也直接设置呢。。

成功了。。。。。。好了,写到这里了。。。拜拜。。




mdzz,怎么可能这么轻松。。。。这个时候要上代码了,要用代码来让他们消失在我们的eyes(我真是有才)里面了。。。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4 全透明状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
           // activity.getWindow().setStatusBarColor(Color.TRANSPARENT);  //直接用这个方法会有兼容性问题
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);//calculateStatusColor(Color.WHITE, (int) alphaValue)
        }
然后在

setContentView(R.layout.activity_main);
之前使用这个,你最好封装一个方法调用咯。。。。看看效果吧:

.。。可以真的到这里就结束了(安心希娜,4.4是没问题的)。。。。。。。。。。。。。。。。。。拜拜

谢谢网上的代码和大神,我是搬运工,搬运了很多。。。。。。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值