EditText弹出软键盘后布局上移问题

本文参考:

http://2dxgujun.com/post/2014/10/23/Soft-Keyboard-Jacking-Control.html

解决其文章的后续其他问题:
这个是问题界面:
EditText弹出软键盘后布局上移问题_第1张图片

这个是布局代码:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/aliceblue"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_dp_40"
        android:background="@color/white"
        android:gravity="center"
        android:text="Title"
        android:textSize="@dimen/dimen_sp_18"/>



        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/antiquewhite"
                android:gravity="center"
                android:text="Content"
                android:textSize="@dimen/dimen_sp_30"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/content"
                android:layout_marginTop="@dimen/dimen_dp_20"
                android:gravity="center"
                android:hint="EditText"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="@dimen/dimen_dp_20"
                android:layout_marginRight="@dimen/dimen_dp_20"
                android:text="Button"/>
        RelativeLayout>
LinearLayout>

使用ScrollView嵌套后并且去掉滑动条,在setContentView()之后添加

//ScrollView去除滑动条
android:scrollbars="none"
//设置Activity的SoftInputMode属性值为adjustResize
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

效果如下:
EditText弹出软键盘后布局上移问题_第2张图片
代码如下:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/aliceblue"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_dp_40"
        android:background="@color/white"
        android:gravity="center"
        android:text="Title"
        android:textSize="@dimen/dimen_sp_18"/>

    <ScrollView
        android:scrollbars="none"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/antiquewhite"
                android:gravity="center"
                android:text="Content"
                android:textSize="@dimen/dimen_sp_30"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/content"
                android:layout_marginTop="@dimen/dimen_dp_20"
                android:gravity="center"
                android:hint="EditText"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_marginBottom="@dimen/dimen_dp_20"
                android:layout_marginRight="@dimen/dimen_dp_20"
                android:text="Button"/>
        RelativeLayout>
    ScrollView>
LinearLayout>

Activity代码如下:

public class EditTextSqueezedTitle extends BaseActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edittext_squeezed_title);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }
}

虽然解决了问题但带来了新的问题,ScrollView的特性影响了我的布局,后来找到方法,只需要在ScrollView属性加上一句代码:

android:fillViewport="true"

默认占满屏幕,并且把ScrollView的唯一子布局RelativeLayout改为LinearLayout不然adjustResize属性
不起作用或者是其他问题这是最终效果图:
EditText弹出软键盘后布局上移问题_第3张图片

这是最终布局代码:


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/aliceblue"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_dp_40"
        android:background="@color/white"
        android:gravity="center"
        android:text="Title"
        android:textSize="@dimen/dimen_sp_18"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/antiquewhite"
                android:gravity="center"
                android:text="Content"
                android:textSize="@dimen/dimen_sp_30"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/dimen_dp_20"
                    android:gravity="center"
                    android:hint="EditText"/>
            LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:layout_marginBottom="@dimen/dimen_dp_20"
                android:layout_marginRight="@dimen/dimen_dp_20"
                android:text="Button"/>
        LinearLayout>
    ScrollView>
LinearLayout>

ScrollView嵌套LinearLayout这一步很重要,不然会引起其他问题,比如底部的Button依然会被软键盘顶起

你可能感兴趣的:(补充)