問題起因:
同組的同事將項目全局設置成了沉浸式,對於我這個半路過來開發的人 可真是頭疼呵~
沒辦法,那就我自己添加一個頭吧。也可以在布局中取消沉浸式,不過我這個是在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
