android 笔记11 得到布局,状态栏显示隐藏,代码优化

  • Post author:
  • Post category:其他


1.在一个普通类中得到activity的布局?

Activity context = view.getContext();

context.findViewById();

private Activity mContext;
mContext = (Activity) view.getContext();

2.动态显示和隐藏系统状态栏

WindowManager.LayoutParams layoutParams = mContext.getWindow().getAttributes();
//显示
layoutParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
            mContext.getWindow().setAttributes(layoutParams);
            mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
//隐藏
layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            mContext.getWindow().setAttributes(layoutParams);
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

3优化后的代码

//直接使用view
View softKeyView = mContext.findViewById(R.id.parent_actions);
        View tabView = mContext.findViewById(R.id.card_tab_layout);
        if (mIsFullScreen) {
            mExpandableSoftKeyPanel.setVisibility(View.VISIBLE);
            softKeyView.setVisibility(View.VISIBLE);
            tabView.setVisibility(View.VISIBLE);
//显示状态栏清除flag
            mContext.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } else {
            mExpandableSoftKeyPanel.setVisibility(View.GONE);
            softKeyView.setVisibility(View.GONE);
            tabView.setVisibility(View.GONE);
//隐藏状态栏清除flag
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }



版权声明:本文为FErryer_linj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。