Android状态栏与布局重叠解决⽅案
问题起因:
同组的同事将项⽬全局设置成了沉浸式,对于我这个半路过来开发的⼈可真是头疼呵~
没办法,那就我⾃⼰添加⼀个头吧。也可以在布局中取消沉浸式,不过我这个是在fragment中,为了不修改之前的代码,只能做此骚操作了。
代码如下:
⽅式⼀:
1、获取状态栏的⾼度。
private int getStatusBarHeight(Context context) {
// 获得状态栏⾼度
int resourceId = Resources().getIdentifier("status_bar_height", "dimen", "android");
Resources().getDimensionPixelSize(resourceId);
}
2、为parentview添加⼀个状态栏⾼度的textview。
TextView textView = new TextView(getContext());
textView.setBackground(getResources().lor.white));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(getContext()));
textView.setLayoutParams(layoutParams);
llLayout.addView(textView, 0);html实现用户注册登录代码
⽅式⼆:
~~偶然看到⼀篇博客上写的,为布局设置距离顶部的⾼度,实现⽅式与上⽂类似,不过是在activity中重写onWindowFocusChanged()⽅法。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
//设置第⼀个view距离状态栏的⾼度;
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) LayoutParams();//rlLinearLayout为遮挡住的页⾯布局LinearLayout
int top =getStatusBarHeight(this);//获取状态栏⾼度
     lp.topMargin = top; rlLinearLayout.setLayoutParams(lp); }
这种⽅式好像也ok.
⽅式三:
在代码中设置:fitsSystemWindows = true即可
参考博⽂地址: