android 实现沉浸式,状态栏

第一步, 配置辅助类

public class ConfigUtils {

private ConfigUtils() {
}

/**
 * 获取状态栏高度![这里写图片描述](//img-blog.csdn.net/20180321101454292?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNzg5MDcz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
 */
public static final int getStatusHeight(Context context) {
    int result = 0;
    int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resId > 0) {
        result = context.getResources().getDimensionPixelOffset(resId);
    }
    return result;
}

/**
 * 判断是否是4.4以上版本, 只有4.4以上版本才有沉浸式
 */
public static boolean hasKitKat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}

/**
 * 由于全屏时转非全屏时界面中的布局大小不能改变,所以不能使用流行的在windows中添加一个
 * Status 高度的控件来填充内容,  直接使用toolbar设置padding效果
 * @param activity
 * @param toolbar
 */
public static void setStatusBarColor(Activity activity, Toolbar toolbar) {
    if (toolbar.getTag() == null) {
        ViewGroup.LayoutParams params = toolbar.getLayoutParams();
        int statusHeight = getStatusHeight(activity);
        params.height += statusHeight;
        toolbar.setLayoutParams(params);
        toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusHeight,
                toolbar.getPaddingRight(), toolbar.getPaddingBottom());
        toolbar.setTag(true);
        //toolbar默认有16dp的margin值,可以设置为0,或在布局中配置
        toolbar.setContentInsetsRelative(0,0);
    }
    if (hasKitKat()) {  //设置显示toolbar时的样式
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }
}

}

示例代码调用

public class TopBarView extends BaseActivity {

private Toolbar mToolbar;
private View iv;
private boolean isfull;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_top_bar_view);
    mToolbar = findView(R.id.m_tool_bar);
    iv=findViewById(R.id.iv);
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (isfull){
                full(false);
                isfull=false;
            }else {
                full(true);
                isfull=true;
            }
        }
    });
    ConfigUtils.setStatusBarColor(TopBarView.this,mToolbar);
    initActionBar();
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

}

private void full(boolean enable) {
    if (!enable){
        //非全屏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        mToolbar.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(TopBarView.this, R.anim.top_translate_enter);
        mToolbar.setAnimation(animation);
        ConfigUtils.setStatusBarColor(TopBarView.this, mToolbar);
        mToolbar.setVisibility(View.VISIBLE);
        animation.start();
    }else{
        mToolbar.clearAnimation();
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.top_translate_exit);
        mToolbar.setAnimation(animation);
        animation.start();
        mToolbar.setVisibility(View.GONE);
        //清除非全屏时的flags,并设置全屏flag
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

private void initActionBar() {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    setSupportActionBar(mToolbar);
    ActionBar bar = getSupportActionBar();
    bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    View actionView = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);
    ActionBar.LayoutParams actionBarParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
    bar.setCustomView(actionView, actionBarParams);
}

}

效果展示:

你可能感兴趣的:(android)