700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android输入法遮挡了输入框 使用android:fitsSystemWindows=true后界面顶部出现白条

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

时间:2024-02-24 12:23:45

相关推荐

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

问题:

1、页面布局文件:

<com.zhy.autolayout.AutoLinearLayout xmlns:android="/apk/res/android"android:id="@+id/layout_order_detail"android:layout_width="match_parent"android:layout_height="match_parent" android:fitsSystemWindows="true"android:orientation="vertical">

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

效果图:

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

解决方法:

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

public class CustomLinearLayout extends AutoLinearLayout {public CustomLinearLayout(Context context) {super(context);}public CustomLinearLayout(Context context, AttributeSet attrs) {super(context, attrs);}public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overrideprotected boolean fitSystemWindows(Rect insets) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {insets.left = 0;insets.top = 0;insets.right = 0;}return super.fitSystemWindows(insets);}@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)@Overridepublic WindowInsets onApplyWindowInsets(WindowInsets insets) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom()));} else {return insets;}}}

2、修改布局文件:

<com.example.widget.CustomLinearLayout xmlns:android="/apk/res/android"android:id="@+id/layout_order_detail"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"android:orientation="vertical">

3、配置文件不设置android:windowSoftInputMode属性;

4、效果图:

问题解决。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。