机器人中调整大小和调整平面的区别?

我尝试编写一个代码,用于在出现 软键盘时重新调整 UI 组件的大小。 当我使用 调整尺寸,时,它重新调整 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>
207888 次浏览

来自 Android 开发者站点链接

“调整尺寸”

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

“调整盘”

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

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

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

正如 医生所说,还要记住正确的价值组合:

该设置必须是下表中列出的值之一, 或者一个“状态...”值加上一个“调整...”值的组合。 在任一组中设置多个值ーー多个“ state...”值, 具有未定义的结果。单个值是分隔的 例如:

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

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

调整大小 = 调整页面内容的大小

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

当我还是个初学者的时候,我对调整大小和调整盘有点困惑。上面给出的定义是正确的。
调整大小: 主要活动的内容被调整大小,以便为软输入(即键盘)腾出空间
调整盘: 不调整窗口的整体内容大小,它只调整内容,以便用户总是可以看到他输入的内容
调整任何东西: 顾名思义,任何东西都没有调整大小或者平底锅。无论是否隐藏内容,键盘都是打开的。< br/>
为了更好的理解,我创造了一个例子
下面是我的 xml 文件: < br/>

<?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 的设计视图
original view

调整规模示例如下:
adjustResize example

调整盘例子如下:
adjustPan example

不做任何调整例子如下:
adjustNothing example

谢谢@Nadeem 给我的灵感。

这可以更直观地说明这种差异。

当键盘不显示时:

noKeyboard

当软键盘显示

Compare differences

在我的模拟器上,调整未指明调整盘有相同的结果。

下面是布局 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>