You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android登录Fragment布局随软键盘弹出上移问题求助

Fixing Layout Shift When Soft Keyboard Appears in Your Login 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:42:59