Android沉浸式状态栏的简单实现

Android4.4之后开始支持透明状态栏,于是有了沉浸式状态栏之说。其实这种叫法是不正确的。参考郭林播客:Android状态栏微技巧,带你真正理解沉浸式模式

今天参考使用第三方库:SystemBatTint,实现沉浸式。

我的理解沉浸式无外乎两种情况,一:纯颜色,二:北京图片延伸到状态栏。

在app/build.gradle中添加依赖:

 compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'

一:纯颜色状态栏的实现

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.readystatesoftware.systembartint.SystemBarTintManager;

public class ColorActivity extends AppCompatActivity implements View.OnClickListener {
    private SystemBarTintManager mTintManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        findViewById(R.id.btn_setBarColor).setOnClickListener(this);
    }

    //点击按钮后,设置状态栏的颜色
    @Override
    public void onClick(View v) {
        //如果是4.4以上版本,将状态栏颜色设置为红色,
        mTintManager = new SystemBarTintManager(this);
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setTintColor( Color.RED);
    }
}
在res中新建values-v19文件夹,res/values-v19/styles.xml

<resources>
    //注意名字要和values中的一致,在AndroidManifest中才能引用到
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>
</resources>
不要忘了在AndroidManifest.xml中引用
 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">




二:背景图片的沉浸式

在布局文件的根标签中添加:

    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:background="@mipmap/dog"
当然也要设置theme,状态栏透明,与上面的最后两步一样。

还有一种简单实现方式:Translucent System Bar 的最佳实践





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值