沉浸状态栏的正确姿势:兼容4.4

1. 两个style文件 style 和 style-v19

1.1style如下:












1.2style-V19如下(两个透明设置)::




2.顶层布局XML文件中加入属性

android:fitsSystemWindows="true"

3. 使用systembarTintmanager

    //设置彩色状态栏
    SystemBarTintManager tintManager = new SystemBarTintManager(this);
    tintManager.setStatusBarTintResource(R.color.colorPrimaryDark);
    tintManager.setStatusBarTintEnabled(true);
    //设置Anctionbar
    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar_floatview);
    setSupportActionBar(mToolbar);

4. 对4.4版本的下移进行处理

使用以上配置在4.4下会整体上移一个systembar的高度,需要在OnCreate方法中加入如下代码:

//4.4系统下移一个状态栏高度
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT)
{
    RelativeLayout mDrawerLayout = (RelativeLayout)         
    findViewById(R.id.linearlayout_floatview);
    mDrawerLayout.setPadding(0, 
    getStatusBarHeight(this.getApplicationContext()), 0, 0);
}

其中,getStatusBarHeight方法通过反射获取SystemBar的高度:

// 通过反射获取状态栏高度
public static int getStatusBarHeight(Context context) {
try {
    Class c = Class.forName("com.android.internal.R$dimen");
    Object obj = c.newInstance();
    Field field = c.getField("status_bar_height");
    int x = Integer.parseInt(field.get(obj).toString());
    return context.getResources().getDimensionPixelSize(x);
} catch (Exception e) {
    e.printStackTrace();
}
return 0;
}

adsfasdfasdfasdf

int void main()

你可能感兴趣的:(沉浸状态栏的正确姿势:兼容4.4)