第一步, 配置辅助类

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;@Overrideprotected 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);}

}

效果展示:

更多相关文章

  1. Android获取系统顶部状态栏(Status Bar)与底部导航栏(Navigation
  2. 动态广告布局
  3. Android自定义添加圆角渐变色按钮+点击效果
  4. 设置android的布局文件的背景颜色为黑色
  5. android 按钮效果的两种实现方法
  6. Android---63---Android中的动画效果
  7. 分享Android仿刮奖效果控件
  8. Android 属性动画实现的扇形菜单效果
  9. android 获取屏幕的宽度和高度

随机推荐

  1. 手动升级 --从 12.1.x.x 到 18.x.x.x 的
  2. MySQL 续集 03
  3. MySQL 续集 04
  4. influxDB安装部署及入门
  5. Oracle如何使用spool导出utf8字符集的文
  6. memcache中的过期时间策略
  7. Linux 下使用 killall 命令终止进程
  8. Oracle 18c - 配置只读 OracleHome / DBC
  9. MySQL 续集 02
  10. 技术问答-20 ArrayList和LinkedList