直接进入主题:
用法:
1:
在oncreate中,调用以下代码块,注:博主因为继承了自己封装的BaseActivity,故写在了重写方法provideBindView()中,其实和在oncreate中一样。(代码在最下面,图为展示)
oncreate中
2:两个方法直接copy到用到的activity地方中。
两个需要引用的方法。
代码列出来:
oncreate中:粗体字都是
private View statusBarView;
//延时加载数据.
Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
@Override
public boolean queueIdle() {
if (isStatusBar()) {
initStatusBar();
getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
initStatusBar();
}
});
}
//只走一次
return false;
}
});
两个引用的方法:
private void initStatusBar() {
if (statusBarView ==null) {
int identifier = getResources().getIdentifier("statusBarBackground", "id", "android");
statusBarView = getWindow().findViewById(identifier);
}
if (statusBarView !=null) {
statusBarView.setBackgroundResource(R.mipmap.home_nav_bg);
}
}
protected boolean isStatusBar() {
return true;
}
注:肯定会有同学好奇我为什么不在Base层中的BaseActivity中调用这些方法。因为我项目中的toolbar是自己封装的一个include布局直接调用的,个别界面的头部,是一张渐变图片,所以在style.xml中设置colorPrimaryDark的值,效果很差。
希望能帮到面部愁容的你,不好使你砍我。