美文网首页
EditText弹出输入键盘后页面往上顶的问题

EditText弹出输入键盘后页面往上顶的问题

作者: hjm1fb | 来源:发表于2017-09-16 17:37 被阅读122次
ps: 在全屏模式下,android:windowSoftInputMode的ajust参数可能不起作用

解决方案:
1.在中AndroidManifest中设置相应的Activity为

android:windowSoftInputMode="adjustPan"

参数介绍:
"adjustResize" 是软件盘在窗口下 窗口会自动调整大小;
当显示软键盘时,调整window内的控件大小以便显示软键盘。这样的话控件可能会变形。
"adjustPan" 是软件盘遮盖在窗口上;
当显示软键盘时,调整window的空白区域来显示软键盘。软键盘还是有可能遮挡一些有内容区域,这时用户就只有退出软键盘才能看到这些被遮挡区域并进行交互。

在选择了adjustPan后,页面还是会往上顶和变形,经过调试得出是 RelativeLayout和 layout_weight 导致的。
猜测是这两个是给控件动态分配空间的,所以当软键盘弹出时,会重新布局页面,导致页面变形。所以建议是有Edit时谨慎使用RelativeLayout 和layout_weight
最后的布局xml如下,用了FrameLayout和LinearLayout。
只在空view中使用layout_weight,用来调节分块的空间占比:
(如果一定要使用RelativeLayout并且不介意父布局变形的话,adjustResize也是可以的)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.duotin.fm.activity.PlayLiveActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

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

        <com.duotin.fm.widget.DTActionBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="230dp"
            android:layout_marginTop="50dp">

            <ImageView
                style="@style/playerCircleImage"
                android:layout_centerInParent="true"
                android:src="@drawable/default_album_image_player" />

            <include
                layout="@layout/real_live_count_down"
                android:layout_width="230dp"
                android:layout_height="230dp"
                android:layout_centerInParent="true"
                android:visibility="invisible" />
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.6"
            android:background="#00ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_horizontal"
            android:background="@drawable/shp_real_live_rounded_retangle_some_transparant"
            android:gravity="center"
            android:minWidth="50dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textSize="15dp"
            android:visibility="invisible" />

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#00ffffff" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_marginBottom="15dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_centerInParent="true"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:src="@drawable/real_live_pause"
                android:visibility="invisible" />

            <RelativeLayout
                android:layout_width="130dp"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/ivPlayControlRL"
                android:background="@drawable/shp_real_live_rounded_retangle">

                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_centerVertical="true"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="12dp"
                    android:layout_marginTop="10dp"
                    android:src="@drawable/real_live_logo" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="5dp"
                    android:layout_toRightOf="@id/ivBrandLogRL"
                    android:gravity="center"
                    android:paddingRight="10dp"
                    android:singleLine="true"
                    android:text=""
                    android:textColor="@color/appOrange"
                    android:textSize="12sp" />
            </RelativeLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#00ffffff" />

            <Button
                android:layout_width="130dp"
                android:layout_height="40dp"
                android:layout_centerInParent="true"
                android:layout_toLeftOf="@+id/ivPlayControlRL"
                android:background="@drawable/shp_real_live_rounded_retangle"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:text="关闭弹幕"
                android:textColor="@color/appOrange"
                android:textSize="12sp" />
        </RelativeLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#00ffffff" />

        <View
            android:layout_width="match_parent"
            android:layout_height="63dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="330dp">

        <master.flame.danmaku.ui.widget.DanmakuView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="50dp" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/sendingTanMu"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_gravity="bottom"
        android:background="#FFEEEEEE"
        android:paddingBottom="10dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="43dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/shp_real_live_right_rounded_retangle"
            android:gravity="center"
            android:padding="10dp"
            android:paddingRight="8dp"
            android:text="发送"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_toLeftOf="@id/tvSendDanmaku"
            android:background="@drawable/shp_real_live_left_rounded_retangle"
            android:hint="你想说什么"
            android:paddingLeft="15dp"
            android:textColorHint="#FF9B9B9B" />
    </RelativeLayout>
</FrameLayout>

也可以参考:
学习windowSoftInputMode,解决软键盘遮挡住输入框的问题

相关文章

网友评论

      本文标题:EditText弹出输入键盘后页面往上顶的问题

      本文链接:https://www.haomeiwen.com/subject/drhusxtx.html