当键盘在FRAGMENT中弹出时,强制移动布局向上

6
我有一个位于**Fragment** 中的 **EditText**。 当键盘弹出时,布局不会改变。 我尝试了以下所有选项,但都没有起作用。 我在stackoverflow上阅读了所有相关主题,但未找到解决方法。 如果有其他想法,请分享。 我尝试过的选项:
<activity android:windowSoftInputMode="adjustPan"> </activity>

<activity android:windowSoftInputMode="adjustPan|adjustResize">

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);


android:windowSoftInputMode="stateVisible|adjustResize"

以下是我的LayoutXML代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layoutDirection="rtl"
    tools:background="@mipmap/bg">


    <RelativeLayout
        android:id="@+id/contactUsSaperator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/gcontactUsHeader"
            android:background="#ffffff" />

        <TextView
            android:id="@+id/gcontactUsHeader"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="@string/contactUs"
            android:textColor="#ffffff" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_toRightOf="@+id/gcontactUsHeader"
            android:background="#ffffff" />


    </RelativeLayout>


    <LinearLayout
        android:id="@+id/contactUsOpenHour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@id/contactUsSaperator"
        android:layout_marginTop="17dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactUsOpenHourText"
            android:textColor="@color/blueTextColor"
            android:textStyle="bold"
            android:textSize="16dp"
            android:layout_marginRight="10dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactUsOpenHourTuesdayWednesday"
            android:textColor="@color/blueTextColor"
            android:textSize="15dp"
            android:layout_marginRight="10dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactUsOpenHourThursday"
            android:textColor="@color/blueTextColor"
            android:textSize="15dp"
            android:layout_marginRight="10dp"
            />
        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/contactUsOpenHourFriday"
            android:textColor="@color/blueTextColor"
            android:textSize="15dp"
            android:layout_marginRight="10dp"
            />  

        <TextView            
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="35dp"
            android:text="@string/contactUsOpenHourPhone"
            android:textColor="@color/blueTextColor"
            android:textStyle="bold"
            android:textSize="16dp"
            android:layout_marginRight="10dp"
            />

        <FrameLayout
            android:layout_marginTop="25dp"
            android:layout_width="wrap_content"
            android:layout_height="150dp">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="150dp"
                android:background="@mipmap/contact_us_tex_fild"
                android:scaleType="fitXY"
                />
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="3dp"
                >
                <EditText
                    android:id="@+id/contactUsEditText"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:hint="@string/contactHint"
                    android:textSize="15dp"

                    android:textColor="@color/blueTextColor"
                    android:textColorHint="@color/blueTextColor"
                    android:inputType="text"
                    />

            </ScrollView>

        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            >
            <ImageView
                android:id="@+id/contactUsSendButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/button"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/contacUsSendButton"
                android:layout_gravity="center"
                android:textColor="@color/blueTextColor"
                android:textSize="22dp"
                android:textStyle="bold"
                />

        </FrameLayout>

    </LinearLayout>

</LinearLayout>

1
尝试仅使用adjustResize android:windowSoftInputMode="adjustResize"进行调整。 - Jai
我尝试了,没有帮助。 - Igor Fridman
只需将ScrollView作为您的布局父级,它就会自动将布局向上移动。 - Imran Betara
这就是我所做的。谢谢 Imran。 并不是自动移动了我的布局,而是我使用了 scrollTo() 方法。 - Igor Fridman
我回答了当前的问题,请点击以下链接查看: https://stackoverflow.com/a/46648553/5223744 - Igor Fridman
2个回答

8

对于碎片:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

针对活动:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

2
scrollView 放置在 linear Layout 中,或者用 scrollview 替换您的 LinearLayout

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接