https://github.com/jgilfelt/SystemBarTint
1.状态栏支持图片显示。
2.状态栏支持颜色变换。
安装:
下载JAR文件如何载入你的工程或者在build.gradle中添加到dependency中,如下所示:
dependencies {
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
}
如何使用:
在theme属性中设置android:windowTranslucentNavigation or android:windowTranslucentStatus 为true或者在代码中设置activity的flags为: FLAG_TRANSLUCENT_NAVIGATION or FLAG_TRANSLUCENT_STATUS。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
判断当前的sdk版本高于19,即Android4.4版本,条件成立才设置沉浸式菜单栏。
private Button mBlueButton;
private Button mGreenButton;
private Button mRedButton;
private SystemBarTintManager tintManager;
private DrawerLayout mDrawerLayout;
private TextView mTextViewContain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scroll_view_demo_activity);
mBlueButton = (Button) findViewById(R.id.mBlueButton);
mGreenButton = (Button) findViewById(R.id.mGreenButton);
mRedButton = (Button) findViewById(R.id.mRedButton);
mTextViewContain = (TextView) findViewById(R.id.mTextViewContain);
mBlueButton.setOnClickListener(this);
mGreenButton.setOnClickListener(this);
mRedButton.setOnClickListener(this);
// 使用条件判断
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
tintManager = new SystemBarTintManager(this);
// 设置状态栏可用
tintManager.setStatusBarTintEnabled(true);
// 设置导航栏不可用
tintManager.setNavigationBarTintEnabled(false);
// 在这获取状态栏的配置信息
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
mTextViewContain.setText("" + config.getStatusBarHeight() + "+" + config.getActionBarHeight());
// Rect outRect = new Rect();
// getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerlayout);
mDrawerLayout.setDrawerListener(this);
}
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);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mBlueButton:
// tintManager.setStatusBarTintColor(Color.BLUE);
tintManager.setTintDrawable(getResources().getDrawable(R.drawable.view_1));
break;
case R.id.mGreenButton:
tintManager.setStatusBarTintColor(Color.GREEN);
break;
case R.id.mRedButton:
tintManager.setStatusBarTintColor(0x33ff0000);
break;
}
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// tintManager.setStatusBarTintColor(0x3300ff00);
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
}
styles.xml文件
设置windowIsTranslucent为true
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowIsTranslucent">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
注:
在activity布局文件中添加属性,让activity适应屏幕和状态栏显示
android:fitsSystemWindows="true"