新建项目,并新建 Abstarct BaseActivity
继承自AppCompatActivity
新建虚方法:
public abstract void initView(Bundle saved);
public abstract void initData();
打开build.gradle文件,加入沉浸式状态栏依赖如下:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
在BaseActivity中完成沉浸式状态栏的初始化,并未Xml布局文件根节点添加如下属性:
沉浸式状态栏初始化:
private SystemBarTintManager systemBarTintManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
initView(savedInstanceState);
initTransparentStatusBar();
initData();
}
/*
* transparent status bar
* */
public void initTransparentStatusBar(){
systemBarTintManager = new SystemBarTintManager(this);
systemBarTintManager.setStatusBarTintEnabled(true);
systemBarTintManager.setNavigationBarTintEnabled(true);
systemBarTintManager.setTintColor(0);
final Drawable drawable = ContextCompat.getDrawable(this, R.color.topBarBgColor);
systemBarTintManager.setStatusBarTintDrawable(drawable);
}
添加属性:
android:clipToPadding="true"
android:fitsSystemWindows="true"
最后修改MainActivity继承自BaseActivity,并实现方法,代码如下:
public class MainActivity extends BaseActivity {
@Override
public void initView(Bundle saved) {
setContentView(R.layout.activity_main);
}
@Override
public void initData() {
}
}
运行效果图如下:
完整源码下载:源码