androidの4.4版本沉浸式透明状态栏与导航栏案例
1.
Android 系统自4.2 开始 UI 上就没多大改变,4.4 也只是增加了透明状态栏与导航栏的功能,如图
看上去的确很好看样子,,接下来看看操作步骤:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_actionbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg2);
}
我们看到在oncreate 方法中,布局xml 不做更改。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
setTranslucentStatus(true);
}
这里是更改的状态栏里透明的;调用方法setTranslucentStatus(true);
@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);
}
然后看到下面有
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg2);
这里有个设置颜色值,即为当前状态栏的颜色。
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg2);
这里有个设置颜色值,即为当前状态栏的颜色。
那么更改了状态栏颜色值后, Actionbar颜色值也要变化啊,,,不急,继续下面更改actionbar颜色
在主配置文件中,
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>
这里background就是actionbar 颜色值啦。。。
以上就实现了完整更改。。。源码群里奉上。。
欢迎进入
android 学习群:191974931