Android登录Fragment布局随软键盘弹出上移问题求助
Hey there! I’ve gone through your code and the problem you’re facing—your layout not shifting up when the keyboard pops up—usually boils down to a couple of common setup issues. Let’s walk through the fixes step by step:
1. Fix the windowSoftInputMode in Manifest
You’re currently using adjustPan|adjustResize together, but these two modes can conflict. adjustPan shifts the entire window up, while adjustResize resizes the layout to make space for the keyboard. For your case, stick with adjustResize alone since it’s designed to adjust the layout’s size:
<activity android:name=".MainActivity" android:label="@string/app_name" android:windowSoftInputMode="adjustResize" > <!-- rest of your intent-filter here --> </activity>
2. Adjust the Root Layout’s Height
Your root ConstraintLayout uses wrap_content for height, which means it only takes up as much space as its content. This leaves no room for the system to resize it when the keyboard appears. Change it to match_parent so the layout fills the entire screen:
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" <!-- Updated from wrap_content --> app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/imageView2">
3. Clean Up Unnecessary Layout Constraints
Your TextInputEditText has constraint attributes (app:layout_constraintEnd_toEndOf, etc.) but since it’s a child of TextInputLayout, these constraints don’t work—they’re meant for direct children of ConstraintLayout. Remove them to avoid confusion, and move the horizontal margins to the parent TextInputLayout instead:
<com.google.android.material.textfield.TextInputLayout android:id="@+id/edit_text_email_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="64dp" <!-- Moved margin here --> android:layout_marginEnd="64dp" <!-- Moved margin here --> app:layout_constraintTop_toBottomOf="@+id/textView8" > <com.google.android.material.textfield.TextInputEditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="55dp" android:layout_marginTop="40dp" android:background="@drawable/email_edit_text" android:hint="example@example.com" android:textAlignment="center" android:textSize="18sp" android:textStyle="bold" /> </com.google.android.material.textfield.TextInputLayout>
Bonus Check: Avoid Fullscreen Themes
If you’re using a fullscreen theme (like Theme.AppCompat.Light.NoActionBar.Fullscreen), adjustResize won’t work because the system assumes the window can’t be resized. To fix this, either switch to a non-fullscreen theme or add this line to your theme’s styles.xml:
<item name="android:windowFullscreen">false</item>
After making these changes, your layout should shift up smoothly when the keyboard is opened, keeping all input fields and buttons visible.
内容的提问来源于stack exchange,提问作者Mood Board

