Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

问题

1、页面布局文件:

1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/layout_order_detail" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:fitsSystemWindows="true" 6 android:orientation="vertical">

2、配置文件不设置android:windowSoftInputMode属性;
效果图:

image

3、加入android:fitsSystemWindows="true"后,解决了输入法遮挡了输入框的问题,但是界面顶部出现了状态栏高度的白条。

解决方法

1、自定义CustomLinearLayout(因为我页面最外层是LinearLayout)继承LinearLayout,重写fitSystemWindows和onApplyWindowInsets两个方法:

1public class CustomLinearLayout extends LinearLayout { 2 public CustomLinearLayout(Context context) { 3 super(context); 4 } 5 6 public CustomLinearLayout(Context context, AttributeSet attrs) { 7 super(context, attrs); 8 } 9 10 public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { 11 super(context, attrs, defStyleAttr); 12 } 13 14 public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 15 super(context, attrs, defStyleAttr, defStyleRes); 16 } 17 18 @Override 19 protected boolean fitSystemWindows(Rect insets) { 20 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 21 insets.left = 0; 22 insets.top = 0; 23 insets.right = 0; 24 } 25 return super.fitSystemWindows(insets); 26 } 27 28 @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH) 29 @Override 30 public WindowInsets onApplyWindowInsets(WindowInsets insets) { 31 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 32 return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom())); 33 } else { 34 return insets; 35 } 36 } 37}

2、修改布局文件:

1<com.example.widget.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/layout_order_detail" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:fitsSystemWindows="true" 6 android:orientation="vertical">

3、配置文件不设置android:windowSoftInputMode属性;
4、效果图:

image.png

问题解决。

点赞
收藏

评论区

加载中...

相关推荐

android ContextMenu 上下文菜单示例

ch2\_contextmenu.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_p

android Notification 状态栏通知使用示例

ch7\_notification.xml:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_

android WebView 使用实例

主布局文件:<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"

360项目

shape资源创建图片(重点)1.resdrawablexxx.xml<shapexmlns:android"http://schemas.android.com/apk/res/android"android:shape"rectangle"

Android toolbar 标题精确居中 不会因返回键偏移

1\.总的布局文件<android.support.design.widget.CoordinatorLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http://schemas.android.com

Android 复选框 以及回显

activity\_main.xml<?xmlversion"1.0"encoding"utf8"?<LinearLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http:/