zoukankan      html  css  js  c++  java
  • Android状态栏与布局重叠解决方案

    问题起因:

    同组的同事将项目全局设置成了沉浸式,对于我这个半路过来开发的人 可真是头疼呵~

    没办法,那就我自己添加一个头吧。也可以在布局中取消沉浸式,不过我这个是在fragment中,为了不修改之前的代码,只能做此骚操作了。

    代码如下:
    1、获取状态栏的高度。

     private int getStatusBarHeight(Context context) {
            // 获得状态栏高度
            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
            return context.getResources().getDimensionPixelSize(resourceId);
        }

    2、为parentview添加一个状态栏高度的textview。

     TextView textView = new TextView(getContext());
      textView.setBackground(getResources().getDrawable(R.color.white));
      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(getContext()));
      textView.setLayoutParams(layoutParams);
      llLayout.addView(textView, 0);

    ~~偶然看到一篇博客上写的,为布局设置距离顶部的高度,实现方式与上文类似,不过是在activity中重写onWindowFocusChanged()方法。

    @Override
        public void onWindowFocusChanged(boolean hasFocus) {
            super.onWindowFocusChanged(hasFocus);
            //设置第一个view距离状态栏的高度;
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rlLinearLayout.getLayoutParams();//rlLinearLayout为遮挡住的页面布局LinearLayout
            int top =getStatusBarHeight(this);//获取状态栏高度
         lp.topMargin = top; rlLinearLayout.setLayoutParams(lp); }

    这种方式好像也ok.

    参考博文地址:

    https://blog.csdn.net/longxuanzhigu/article/details/77977835

  • 相关阅读:
    优先队列
    Problem W UVA 662 二十三 Fast Food
    UVA 607 二十二 Scheduling Lectures
    UVA 590 二十一 Always on the run
    UVA 442 二十 Matrix Chain Multiplication
    UVA 437 十九 The Tower of Babylon
    UVA 10254 十八 The Priest Mathematician
    UVA 10453 十七 Make Palindrome
    UVA 10163 十六 Storage Keepers
    UVA 1252 十五 Twenty Questions
  • 原文地址:https://www.cnblogs.com/fangg/p/11969567.html
Copyright © 2011-2022 走看看