在styles.xml里增加TranslucentTheme,我们这里minSdkVersion 是以21为准,低于安卓5.0以下的手机很少了,就不适配了。
对于这种没有标题栏,图片沉浸到状态栏的效果,我们已经实现了。如果是有标题栏呢?比如加个Toolbar会变成下面这样:
对于有标题的页面,我们希望状态栏颜色跟标题栏一样就行了,不希望标题栏上移跟状态栏重叠,我们可以在布局文件根视图设置如下属性,这个相当于设置了个padding让状态栏下移,当然,为了让状态栏颜色跟标题栏一样,你还需要给根视图设置一样的背景色(因为状态栏实际是透明的)。
运行看看,已经实现了我们的要求。
studio,中引入沉浸式兼容库compile ‘com.readystatesoftware.systembartint:systembartint:1.0.3’
eclipse,可以导入相应的那个类。
第一类,兼容actionbar
第一步:设置activity主题android:theme=”@style/ActionBarTheme”
<style name="ActionBarTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
<item name="android:actionBarStyle">@style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="android:Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@color/actionbar_bg</item>
</style>
第二步:设置状态栏透明,然后设置状态栏沉浸的颜色
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow()
WindowManager.LayoutParams winParams = win.getAttributes()
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
if (on) {
winParams.flags |= bits
} else {
winParams.flags &= ~bits
}
win.setAttributes(winParams)
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true)
}
SystemBarTintManager tintManager = new SystemBarTintManager(this)
tintManager.setStatusBarTintEnabled(true)
//设置沉浸的颜色tintManager.setStatusBarTintResource(R.color.statusbar_bg)}
第三步:设置适应windows,在布局文件设置
android:fitsSystemWindows=”true”
如果不设置,应用的ui会顶上去,顶进system ui
ok
第二类 没有actionbar的activity
第一步,设置主题,android:theme=”@style/FullBleedTheme”
<style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- API 14 theme customizations can go here. -->
</style>
<style name="FullBleedTheme" parent="android:Theme.Holo.Light.NoActionBar.TranslucentDecor">
<!-- API 19 theme customizations can go here. -->
</style>
或者
用toolbar只能设置Theme.AppCompat.NoActionBar主题
<style name="AppThemeToolbar" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#2196F3</item>
<!--<item name="colorPrimaryDark">#1565C0</item>-->
<item name="colorAccent">#E91E63</item>
</style>
第二步:同上一个第二步。
设置状态栏透明+颜色
mTintManager = new SystemBarTintManager(this)
mTintManager.setStatusBarTintEnabled(true)
mTintManager.setNavigationBarTintEnabled(true) mTintManager.setStatusBarTintResource(R.color.statusbar_bg)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)