沉浸式状态栏SystemBarTint、实现状态栏半透明

https://blog.csdn.net/u011228356/article/details/44061073

沉浸式状态栏SystemBarTint(Android4.4以上适用)

添加依赖:

implementation ‘com.readystatesoftware.systembartint:systembartint:1.0.4’

布局文件

在最顶层的布局(LinearLayout等)里添加

android:fitsSystemWindows="true"
android:clipToPadding="false"

fitsSystemWindows:让view根据系统窗口(如status bar)来调整自己的布局,值为true,则调整view的paingding属性来给system windows留出空间。

clipToPadding: 控件的绘制区域是否在padding里面,默认为true,为false表示可以在控件外面显示。

xml文件代码:


 
    
 

Activity中:

因为该控件在4.4及以上版本才适用,需要增加版本判断

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    setTranslucentStatus(true);
}

activity代码:

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.setNavigationBarTintEnabled(true);
 
    }
 
    @TargetApi(19)
    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);
    }
}

 

你可能感兴趣的:(Android)