GitHub
Demo 源码
1. 首先看下状态栏
与导航栏
结构图
主要设置以下两个颜色一致
- **colorPrimary **
- colorPrimaryDark
2. 引用库 SystemBarTint, 并在module / build.gradle
里添加以下依赖:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
3. 写一个公共的BaseActivity
, 只要继承BaseActivity
即可实现沉浸式效果
package androidautolayout.yjn.com.statusbar;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.colorPrimary);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
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);
}
}
Avtivity
直接 extendsBaseActivity
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}