美文网首页
Android沉浸式状态栏

Android沉浸式状态栏

作者: yangjianan | 来源:发表于2016-03-25 17:04 被阅读591次

    GitHubDemo 源码

    1. 首先看下状态栏导航栏结构图

    主要设置以下两个颜色一致

    1. **colorPrimary **
    2. colorPrimaryDark
    状态栏结构.jpg

    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直接 extends BaseActivity

    public class MainActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
        }
    }
    

    4.注册清单设置 AndroidManifest.xml

    Paste_Image.png

    相关文章

      网友评论

          本文标题:Android沉浸式状态栏

          本文链接:https://www.haomeiwen.com/subject/ljcdlttx.html