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;
         }
     }
}

转载:http://www.jianshu.com/p/773f6e6ab972
自己也记录一下。假如涉及到版权 请联系 删除 谢谢

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当 RecyclerView 的 EditText 获取焦点并弹起软键盘时,可能会出现软键盘遮挡了 EditText 的情况,这个问题可以通过以下几种方式解决: 1. 使用 adjustResize 属性 在 Activity 的 AndroidManifest.xml 文件,将 Activity 的 windowSoftInputMode 属性设置adjustResize,即可在软键盘弹出时自动调整布局,将 EditText 上移以避免被遮挡。 ```xml <activity android:name=".YourActivity" android:windowSoftInputMode="adjustResize"> ... </activity> ``` 2. 使用 android:fitsSystemWindows 属性 在 RecyclerView 的父布局添加 android:fitsSystemWindows="true" 属性,这样可以让 RecyclerView 的布局留出足够的空间来显示软键盘,避免 EditText 被遮挡。 ```xml <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> ``` 3. 使用 scrollToPosition 或 scrollToPositionWithOffset 方法 在 RecyclerView 的 EditText 获取焦点时,手动将 RecyclerView 滚动到 EditText 所在的位置,这样就可以避免 EditText 被遮挡。 ```java // 获取 EditText 在 RecyclerView 的位置 int position = mAdapter.getPosition(editText); // 滚动 RecyclerView 到指定位置 mRecyclerView.scrollToPosition(position); // 或者使用 scrollToPositionWithOffset 方法,更精确地定位到 EditText 的位置 int offset = 100; // 偏移量,可根据实际情况调整 mLayoutManager.scrollToPositionWithOffset(position, offset); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值