背景
前两天看微信更新后,我自认为微信的沉浸式导航栏挺好看的,小编就想,我也把这种样式运用到我的项目如何呢?下面就一起来探讨一下,微信导航栏大家都知道,就不上图了,直接上我自己完成的效果。
主题字体颜色为黑色,导航栏background设置为透明色,大致就是这么一个思路,还有另外一种方法:通过java代码进行设置
if (Build.VERSION.SDK_INT >= 21) {//21表示5.0
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
//windowLightStatusBar
} else if (Build.VERSION.SDK_INT >= 19) {//19表示4.4
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//虚拟键盘也透明
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
我觉得这种方式是能成功,但还是不是我想要的,另外还有第三方框架来实现,这里就不多讲。
除了以上两种以外,还有一种方法那只能通过style->设置主题样式来进行设置了(我采用的就是这一种方式),下面直接上代码
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="colorAccent">@color/colorPrimary</item>
<item name="windowNoTitle">?android:windowNoTitle</item>
<item name="android:background">@color/transparency</item>
<item name="android:windowLightStatusBar" tools:ignore="NewApi">true</item>//这个属性就是改变字体的颜色
<item name="android:windowTranslucentStatus" tools:ignore="NewApi">true</item>//是否将窗口设为透明状态
</style>
<style name="AppTheme_Slide" parent="AppTheme">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
</style>
这里设置后有个问题就是app内部的导航栏就会太靠上,显得不美观,那么小编的解决方式如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/colorPrimary"
android:layout_height="25dp">
</LinearLayout>
将LinearLayout高度设为固定值(不想这样也可以通过得到手机的导航栏进行计算也可实现,在这里小编就算是偷懒吧),再将这个布局引入其他界面即可解决上述所遇到的问题。
好啦,就这么简单,我只是一个菜鸟,也是自己记录一下。希望看到的同学给我一个赞吧,共勉之。