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">
二:背景图片的沉浸式在布局文件的根标签中添加:
当然也要设置theme,状态栏透明,与上面的最后两步一样。android:clipToPadding="false" android:fitsSystemWindows="true" android:background="@mipmap/dog"
还有一种简单实现方式:Translucent System Bar 的最佳实践。