and*_*ter 115 android android-softkeyboard
我尝试编写一个代码,用于在出现软键盘时重新调整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>
Run Code Online (Sandbox Code Playgroud)
和清单文件:
<?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>
Run Code Online (Sandbox Code Playgroud)
sti*_*ike 202
"adjustResize"
活动的主窗口始终调整大小,以便为屏幕上的软键盘腾出空间.
"adjustPan"
活动的主窗口未调整大小以便为软键盘腾出空间.相反,窗口的内容会自动平移,以便键盘不会遮挡当前焦点,用户可以随时看到他们正在键入的内容.这通常比调整大小更不合适,因为用户可能需要关闭软键盘以获得窗口的模糊部分并与其交互.
根据您的评论,在您的活动清单中使用以下内容
<activity android:windowSoftInputMode="adjustResize"> </activity>
Run Code Online (Sandbox Code Playgroud)
Nad*_*ikh 39
当我是初学者时,我也对 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>
Run Code Online (Sandbox Code Playgroud)
这是xml的设计视图
下面的 AdjustResize 示例:
下面的 AdjustPan 示例:
AdjustNothing 下面的例子:
Cha*_*ler 24
感谢@Nadeem 的灵感。
这样可以更直观地体现出差异。
当键盘不显示时:
当软键盘显示时
在我的模拟器上,adjustmentUnspecified与adjustmentPan具有相同的结果。
这是布局 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>
Run Code Online (Sandbox Code Playgroud)
正如doc所说,还要记住正确的值组合:
该设置必须是下表中列出的值之一,或者是一个"state ..."值加上一个"adjust ..."值的组合.在任一组中设置多个值 - 例如多个"state ..."值 - 具有未定义的结果.各个值由垂直条(|)分隔.例如:
<activity android:windowSoftInputMode="stateVisible|adjustResize" . . . >
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
在AndroidManifest.xml 中使用当前活动,并android:fitsSystemWindows="true"
在样式或rootLayout中使用.
归档时间: |
|
查看次数: |
128635 次 |
最近记录: |