Android EditText弹出软键盘实现页面标题头不动,软键盘弹出在编辑框下面

为了实现EditText编辑的时候弹出软键盘标题头不动,底部编辑框上移,想了好多种方法,也百度,问同事每种办法都有问题,在这总结一下,希望能帮助到大家。
上图看下效果:
弹出前
弹出后
可以看到弹出键盘的时候,只有EditText在软键盘上面,还有一个蓝色点,这个随后再说。
用RelativeLayout实现的父布局id为root,试验过很多种方法,总结一下,想要实现这种效果,有一个原则 EditText所在的布局中,必须位于root的底部,也就是写android:layout_alignParentBottom=”true”这个属性,然后再根据具体情况调整布局。
为了更简单的实现功能,页面算是最简单的实现了,现在说一下那个蓝色的点,他是位于EditText上面也就是用了above属性,所以我们可以看到它也被弹到布局上面。
总结一下规律就是,RelativeLayout在弹出软键盘的时候先寻找android:layout_alignParentBottom属性是否有控件设置为true,如果有将此控件向上移动键盘高度的位置,布局也就位于软键盘的上面,其他控件如果有相对于该控件的位置,也就相对的移动了,如果没有则什么都不做,可以看做布局是一层一层盖上去的,键盘弹出的时候,只把符合要求的当层的布局向上移动,所以如果我们按照这种方法写,肯定是可以的。
还有一个重点就是,配置文件里面该activity要设置android:windowSoftInputMode=”adjustResize”
相信还有小伙伴会遇到设置之后不生效的结果,那就在布局文件的根布局添加android:fitsSystemWindows=”true”属性,但是有的还会有问题,如果代码中有设置状态栏颜色的,会多出一条状态栏高度的空白条,这个还不知道如何解决。
也是经过很多实验发现的,表述不是很清楚,不懂得可以沟通,希望可以帮助到大家吧,毕竟感觉这个坑不大不小,但却挺烦人的。有大神也可以给出更权威的答案,相互学习,共同进步。
在此附上代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/white">

    <include
        android:id="@+id/ll_title_bar"
        layout="@layout/appbar"/>

    <LinearLayout
        android:id="@+id/ll_center"
        android:layout_width="match_parent"
        android:layout_below="@+id/ll_title_bar"
        android:orientation="vertical"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 111"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 222"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 333"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 4444"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 555"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 6666"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 777"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test 888"
            android:layout_margin="10dp"
            android:padding="10dp"
            android:background="@color/error_stroke_color"
            />
    </LinearLayout>

    <EditText
        android:id="@+id/et_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:hint="EditText"
        android:background="@color/blue_btn_bg_color"
        android:padding="10dp"
        />

    <ImageView
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:layout_above="@+id/et_test"
        android:layout_centerHorizontal="true"
        android:background="@color/colorPrimary"/>


</RelativeLayout>

大家可以改一下布局,会发现只有蓝色的点向上移动了
正常的代码

猜你喜欢

转载自blog.csdn.net/qq_34198206/article/details/64444872