Android中adjustResize失效的解決辦法之一


今天幫助哥們解決了一個比較蛋疼的問題,就是在有的情況下會出現設置activity的windowSoftInputMode="adjustResize"時,會失效的情況。

歷盡千辛萬苦,終於在stackflow上找到解決方法。在activity的根布局上添加fitsSystemWindows="true".

然后adjustResize就可以成功的起作用了。但是在這種情況下,你的titlebar會下移statusbar的高度的距離。所以就必須重寫一個layout繼承自你所使用的layout,並重寫其中的兩個方法,貼出代碼:

 public class MyLinearLayout extends LinearLayout{

private int[] mInsets = new int[4];

public MyLinearLayout(Context context) {
super(context);
}

public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected final boolean fitSystemWindows(Rect insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Intentionally do not modify the bottom inset. For some reason,
// if the bottom inset is modified, window resizing stops working.
// TODO: Figure out why.

mInsets[0] = insets.left;
mInsets[1] = insets.top;
mInsets[2] = insets.right;

insets.left = 0;
insets.top = 0;
insets.right = 0;
}

return super.fitSystemWindows(insets);
}

@Override
public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
mInsets[0] = insets.getSystemWindowInsetLeft();
Log.e("mInsets[0]",""+mInsets[0]);
mInsets[1] = insets.getSystemWindowInsetTop();
Log.e("mInsets[1]",""+mInsets[1]);
mInsets[2] = insets.getSystemWindowInsetRight();
Log.e("mInsets[2]",""+mInsets[2]);
return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
insets.getSystemWindowInsetBottom()));
} else {
return insets;
}
}
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com