此布局具有两个输入字段和一个按钮。调整布局大小时(由于在输入字段中显示了软键盘),先前与布局底部对齐的按钮被绘制在输入字段上。这里的目标是确保所有视图在小屏幕上都不会重叠。在大屏幕上,调整窗口大小的调整是完美的。
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/long_message"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/top_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/message">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/bottom_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/top_input">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/cta"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/bottom_input" />
<Button
android:id="@+id/cta"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="@string/forward"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我试图放入一个Space视图(就像在粘贴的XML中一样)只是为了添加约束,这样就不会发生这种情况,但是它似乎被忽略了。我尝试添加一个准则,但是它需要一个特定的百分比或距离,此处不适用。
什么是正确的方法?令人惊讶的是,那里的空间视图约束被忽略了。
您可以在下面执行此操作首先只需将android:windowSoftInputMode="adjustResize"
添加到您的相关活动中。
然后您只需要把
[android:fillViewport="true"
进入您的NestedScrollView
,它将正常工作请尝试下面的XML代码。
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="What's the right way to do this? It really surprises me that the Space view's constraints there are being ignored."
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/top_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/message">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/bottom_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/top_input">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/cta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="forward"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/bottom_input"
app:layout_constraintVertical_bias="0.995" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Space view
无法自行解决您想要的东西。我实现了所需的布局,以避免出现软键盘时视图重叠。
起初],我确实创建了相应的布局。我试图使其尽可能可读,以便使所有人容易理解。
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 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"> <ScrollView android:id="@+id/scroll" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_constraintBottom_toTopOf="@+id/top_input" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/this_is_a_big_text" /> <TextView android:id="@+id/big_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/this_is_a_big_text" /> </LinearLayout> <EditText android:id="@+id/top_input" android:layout_width="match_parent" android:layout_height="70dp" android:hint="Top input !" app:layout_constraintBottom_toTopOf="@+id/bottom_input" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/bottom_input" android:layout_width="match_parent" android:layout_height="70dp" android:hint="Bottom input !" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/top_input" /> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="70dp" android:text="Click Me !" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/bottom_input" /> </androidx.constraintlayout.widget.ConstraintLayout> </ScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
如您所见,我已经添加了
ScrollView
并将其放入ConstraintLayout
。这是因为,我想使ConstraintLayout
的大小可调整。
下一个>>,当出现软输入时,我们将在manifest
中设置活动行为。
,我们还需要一件事,即在出现软输入并调整布局大小时,滚动视图才能结束。<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.input_with_softkeyboard"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" tools:ignore="GoogleAppIndexingWarning"> <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize|stateVisible"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
[正如您所看到的,我们已经设置
android:windowSoftInputMode="adjustResize|stateVisible"
。因此,当出现软输入时,布局将被调整大小。请记住,如果您未设置adjustResize
,软输入将在视图上重叠。ConstraintLayout
中的ScrollView
不会被调整大小。finally
这里是一个很好用且易于使用的库,让我们拥有KeyboardVisibilityEvent的软输入侦听器>
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ScrollView scroll = findViewById(R.id.scroll); KeyboardVisibilityEvent.setEventListener( this, new KeyboardVisibilityEventListener() { @Override public void onVisibilityChanged(boolean isOpen) { scroll.post(new Runnable() { @Override public void run() { scroll.fullScroll(View.FOCUS_DOWN); } }); } }); } }
当出现软输入时,我们会将视图向下滚动
您将拥有的是