Android 4.4沉浸式状态栏的实现

要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint)

public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.drawable.back);

    }

    @TargetApi(19)
    public 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);
    }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
      
    android:fitsSystemWindows="true"  
    android:clipToPadding="true"  
  
    android:background="#ffffff"  
    android:orientation="vertical"  
>  
  
  
  
    <TextView  
        android:layout_width="match_parent"  
        android:layout_height="100dp"  
        android:background="#009959" />  
  
  
    <Button  
        android:layout_width="100dp"  
        android:layout_height="50dp"  
        android:background="#ff669d"/>  
  
</LinearLayout>  
android:fitsSystemWindows="true"  :设置应用布局时是否考虑系统窗口布局;如果为true,将调整系统窗口布局以适应你自定义的布局。比如系统有状态栏,应用也有状态栏时。
android:clipToPadding="true"  : 属性定义了是否允许ViewGroup在padding中绘制,该值默认为true,即不允许。


效果如下:






原文地址:https://www.cnblogs.com/l2rf/p/4766733.html