Android 透明色状态栏

版权声明:> 本文为吕氏春秋i的原创文章,未经博主允许不得转载。 https://blog.csdn.net/Life_s/article/details/81913368

效果图:
这里写图片描述

状态栏改为透明色 你需要注意一下几点:
在style中设置属性:

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

在注册Activity的时候将主题设置进去
例如:

 <activity
            android:name="com.tencent.connect.common.AssistActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="behind"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

注意:不设置的话 将默认使用系统主题

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

将你希望有透明状态栏的活动/容器布局需要设置此属性:

android:fitsSystemWindows="true"

在视图初始化的地方如:Activity's onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window w = getWindow(); 
            w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        }

android API小于19的时候在dimens文件里需要 手动添加

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="padding_status_bar_top">0dp</dimen> //添加这句为0dp    
</resources>

API大于19的时候:
需要新建values-v19文件夹,新建dimens文件 系统默认statusBar高度为25dp

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="padding_status_bar_top">25dp</dimen>
</resources>
与君共勉

我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天
这里写图片描述

猜你喜欢

转载自blog.csdn.net/Life_s/article/details/81913368
今日推荐