在安卓系统中,adjustResize和adjustPan有什么区别?

184

我尝试编写一段代码,用于在软键盘出现时调整UI组件的大小。当我使用adjustResize时,它会重新调整UI组件的大小,同时adjustPan也给了我同样的输出。我想知道它们之间的区别以及何时使用每个组件?哪一个(adjustPan或adjustResize)更适合调整UI大小?

这是我的XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical" >

            <EditText
                android:id="@+id/editText5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="45dp"
                android:ems="10"
                android:inputType="textPersonName" />

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="40dp"
                android:text="My Button" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

以及清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.adjustscroll"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.adjustscroll.MainActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1
对于那些在ReactNative工作(以及可能是本机Android)的人,请不要忘记在更改AndroidMainfest文件时重新构建应用程序。 - Filip Savic
6个回答

294

Android开发者网站链接

"adjustResize"

该活动的主窗口总是调整大小以为软键盘留出空间。

"adjustPan"

该活动的主窗口不会调整大小以为软键盘留出空间。相反,窗口内容将自动滚动,以使当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常比调整大小更不可取,因为用户可能需要关闭软键盘才能访问和与窗口的遮蔽部分进行交互。

根据您的评论,在活动清单中使用以下内容

<activity android:windowSoftInputMode="adjustResize"> </activity>

2
谢谢您的友好回复。现在我遇到了一个UI问题。我的布局底部有一个EditText和一个Button。当软键盘出现时,我想将它们显示在软键盘上方。我该如何实现这个功能?您能帮我解决这个问题吗? - androidcodehunter
2
这对我不起作用。实际上我想要的是:当我尝试在Edittext字段中写入内容时,我只想显示Edittext和Button在软键盘上方。我尝试了你的解决方案但没有起作用。 - androidcodehunter
2
adjustResize 只有在 UI 可调整大小时才提供解决方案,但如果我放置5个以上的编辑文本,则无法正常工作。当 UI 不可调整大小时,是否可能在软键盘上方显示按钮和编辑文本。实际上,这个问题是在我测试这个问题时产生的。 - androidcodehunter
这通常不如调整大小来得理想。但是,如果调整大小会导致视图重叠,则不是这样的。例如,当您有一个选项卡导航和每个选项卡片段中的一堆视图时,在某些设备上,当键盘显示时,adjustResize可能会导致视图重叠。在这种情况下,不幸的是,您必须设置adjustPan以防止发生这种情况。 - IgorGanapolsky
1
@support_ms 为什么需要将"adjustResize"替换为"adjustPan"? - IgorGanapolsky
显示剩余10条评论

64

当我还是一个初学者时,我对于adjustResize和adjustPan之间的区别也感到有些困惑。上面给出的定义是正确的。
AdjustResize:主活动的内容会被重新调整大小以为软输入即键盘腾出空间
AdjustPan:它不会改变窗口的整体内容大小,而是只是平移内容,以便用户始终可以看到自己正在输入什么。
AdjustNothing:顾名思义,没有任何内容被重新调整大小或平移。无论键盘是否隐藏内容,它都会打开。

我创建了一个示例以便更好地理解。
以下是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:hint="Type Here"
        app:layout_constraintTop_toBottomOf="@id/button1"/>


    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/button2"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginBottom="@dimen/margin70dp"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/button1"
        app:layout_constraintEnd_toStartOf="@id/button3"
        android:layout_marginBottom="@dimen/margin70dp"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button3"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/button2"
        android:layout_marginBottom="@dimen/margin70dp"/>
</android.support.constraint.ConstraintLayout>

这是xml的设计视图:
原始视图

下面是AdjustResize的示例:
adjustResize示例

下面是AdjustPan的示例:
adjustPan示例

下面是AdjustNothing的示例:
adjustNothing示例

53

adjustResize = 调整页面内容大小

adjustPan = 移动页面内容而不调整页面大小


adjustResize,我必须重新安装应用程序才能使其正常工作(工作=不重叠),难道只有我一个人这样吗? - Dimitri Kopriwa

22

感谢 @Nadeem 的灵感。

这可能更直观地展示了差异。

当键盘未显示时:

noKeyboard

当软键盘出现时:

Compare differences

在我的模拟器上,adjustUnspecifiedadjustPan 有相同的结果。

这是布局 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- This layout takes all the reset of the screen -->
    <LinearLayout
        android:id="@+id/layout_colors"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/et_input_field"
        >
        <!-- Divide the layout into 3 different colors -->
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_green_light"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_blue_light"
            />
    </LinearLayout>

    <EditText
        android:id="@+id/et_input_field"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:imeOptions="actionDone"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>


4
有插图的话,事情会变得容易很多,谢谢。 - alizeyn

12

文档所述,请记住正确的值组合:

该设置必须是以下表中列出的值之一,或者一个“state…”值和一个“adjust…”值的组合。在任一组中设置多个值(例如多个“state…”值)会产生未定义的结果。各个值由竖线(|)分隔。例如:

<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >

1
无法工作,我的屏幕被软输入法遮挡了。 - MSD

8
你可以在AndroidManifest.xml中的当前Activity使用 android:windowSoftInputMode="stateAlwaysHidden|adjustResize",并在styles或rootLayout中使用android:fitsSystemWindows="true"

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