安卓 android:windowsoftinputmode,Android:windowSoftInputMode="adjustResize"无效解决方法
Android:windowSoftInputMode="adjustResize"無效解決方法
時間:2018-08-16?????來源:未知
Android開發中用到軟鍵盤時會出現設置Activity的windowSoftInputMode="adjustResize"失效的問題,這時我們可以在activity的根布局上添加fitsSystemWindows="true",然后adjustResize屬性就會起作用。但是這樣做還有一個問題,我們的Actionbar會下移一段距離,這段距離的高度相當于狀態欄的高度。那么我們該如何解決這個問題,完美使用android adjustresize屬性呢?
解決這個問題我們需要自定義一個layout做我們的根布局,在layout中重寫fitSystemWindows()和onApplyWindowInsets()方法,具體如下:
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;
}}}
經過以上的操作之后,就可以很完美的解決windowSoftInputMode="adjustResize"無效問題了,分享就到這里,如果對大家有所幫助,請繼續關注我們,我們會持續更新更多優質技術文章!
總結
以上是生活随笔為你收集整理的安卓 android:windowsoftinputmode,Android:windowSoftInputMode="adjustResize"无效解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android进程间对象传递,Andro
- 下一篇: android 两足机器人,基于Andr