【问题标题】:Difference between adjustResize and adjustPan in android?android中adjustResize和adjustPan的区别?
【发布时间】:2013-07-01 17:44:55
【问题描述】:

我尝试编写一个代码,用于在 软键盘 出现时重新调整 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>

【问题讨论】:

  • 对于那些在 ReactNative(也可能是原生 Android)中工作的人 - 不要忘记在更改 AndroidMainfest 文件时重新构建应用程序。

标签: android android-softkeyboard


【解决方案1】:

来自Android Developer Site link

“调整大小”

Activity 的主窗口总是会调整大小,以便为软体腾出空间 屏幕上的键盘。

“调整盘”

活动的主窗口未调整大小以为软体腾出空间 键盘。而是自动平移窗口的内容 这样当前的焦点就不会被键盘和用户遮挡 总能看到他们在输入什么。这通常不太理想 而不是调整大小,因为用户可能需要关闭软键盘才能 到达窗口的模糊部分并与之交互。

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

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

【讨论】:

  • 感谢您的精彩回复。现在我的用户界面有问题。我的布局底部有一个编辑文本和一个按钮。当软键盘出现时,我想在软键盘上方显示那些。我怎样才能做到这一点。你能帮我解决这个问题吗?
  • 这对我不起作用。实际上我想要的是:当我尝试在 Edittext 字段中写东西时,我只想在软键盘上方显示 Edittext 和 Button。我尝试了您的解决方案,但无法正常工作。
  • 哎呀..它对我有用..无论如何..可以使用滚动视图吗?
  • adjustResize 仅在 UI 可用于重新调整大小时提供解决方案,但如果我再放 5 个 edittext 则它无法工作。当 UI 无法重新调整大小时,是否可以在软键盘上方显示按钮和编辑文本。实际上,当我测试这个问题时,问题就出现了。
  • @support_ms 为什么~"replace adjustResize to adjustPan" 是必要的?
【解决方案2】:

adjustResize = 调整页面内容的大小

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

【讨论】:

  • adjustResize 我必须重新安装应用程序才能让它工作(工作 = 没有重叠),只有我一个人吗?
【解决方案3】:

当我还是初学者的时候,我也对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 示例如下:


AdjustPan 示例如下:


AdjustNothing 以下示例:

【讨论】:

    【解决方案4】:

    正如doc 所说,还要记住正确的值组合:

    设置必须是下表中列出的值之一, 或一个“状态...”值加上一个“调整...”值的组合。 在任一组中设置多个值 - 多个“状态...”值, 例如 - 具有未定义的结果。单独的值是分开的 通过竖线 (|)。例如:

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

    【讨论】:

    • 不工作,我的屏幕在软输入后面
    【解决方案5】:

    您可以在 AndroidManifest.xml 中为您当前的活动使用android:windowSoftInputMode="stateAlwaysHidden|adjustResize", 并在样式或 rootLayout 中使用 android:fitsSystemWindows="true"

    【讨论】:

      【解决方案6】:

      感谢@Nadeem 的启发。

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

      键盘未显示时:

      软键盘显示时

      在我的模拟器上,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>
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-27
        • 1970-01-01
        • 1970-01-01
        • 2012-01-13
        • 1970-01-01
        • 2017-09-02
        相关资源
        最近更新 更多