使用这个库,首先需要添加依赖:
compile 'org.zackratos:ultimatebar:1.0.3'
自定义颜色的状态栏和导航栏
要设置自定义颜色的状态栏和导航栏只需要在 onCreate 方法中调用如下代码:
UltimateBar ultimateBar = new UltimateBar(this); ultimateBar.setColorBar(ContextCompat.getColor(this, R.color.DeepSkyBlue));
半透明的状态栏和导航栏
半透明状态栏和导航栏的使用方法也非常简单,只要在 onCreate 方法中调用以下代码:
UltimateBar ultimateBar = new UltimateBar(this); ultimateBar.setTransparentBar(Color.BLUE, 50);
完全透明的状态栏和导航栏
其实完全透明的状态栏和导航栏就是半透明的状态栏和导航栏中当透明度为 0 的情况,只需在 onCreate 方法中调用如下方法:
UltimateBar ultimateBar = new UltimateBar(this); ultimateBar.setImmersionBar();
隐藏状态栏和导航栏
这种情况比较常见了,一般玩游戏,看视频就是这种效果,这种效果的实现有点特殊,必须重写 Activity 的 onWindowFocusChanged 方法,如下:
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { UltimateBar ultimateBar = new UltimateBar(this); ultimateBar.setHintBar(); } }
针对 DrawerLayout 的实现
还有一种特殊情况,就是对于 DrawerLayout,上面的方法会出现一些问题,达不到想要的效果,这里针对 DrawerLayout 做了特殊处理,一般来说,对于 DrawerLayout 只要实现自定义颜色的状态栏和导航栏效果就好了,其他情况就不用考虑了,可以在 onCrate 调用如下代码:
UltimateBar ultimateBar = new UltimateBar(this);
ultimateBar.setColorBarForDrawer(ContextCompat.getColor(this, R.color.DeepSkyBlue));
但是这样其实还是不够的,还必须要在布局文件中在 DawerLayout 的子 view 的主界面添加 android:fitsSystemWindows="true",就像这样:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/SpringGreen"
android:layout_gravity="left"/>
</android.support.v4.widget.DrawerLayout>