How to prevent one activity from resizing when keyboard opens

ε祈祈猫儿з 提交于 2020-05-24 05:21:50

问题


I want to prevent my activity from resizing when my keyboard opens.

I know I can use android:windowSoftInputMode="adjustNothing" in my manifest folder to do that, but it applies the change to all of my activities.

How can I apply this change to only one of my activities?

I tried to add android:windowSoftInputMode="adjustNothing" to my activity but it didn't work.


回答1:


Try this layout:

<?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">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/entrytext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Please Enter your Google Account"
                android:textAlignment="center"
                android:textSize="18dp"
                android:textStyle="bold" />


            <AutoCompleteTextView
                android:id="@+id/emailtext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:textColorHint="#80000000" />

            <EditText
                android:id="@+id/passwordtext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:hint="Password"
                android:inputType="textPassword"
                android:textColorHint="#80000000" />

            <Button
                android:id="@+id/enter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Enter"
                android:textAllCaps="false" />



        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>



回答2:


Try to add in your code in onCreate

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

In case you are using a scroll view please add this

android:isScrollContainer="false"

Hope this works for you....



来源:https://stackoverflow.com/questions/61095575/how-to-prevent-one-activity-from-resizing-when-keyboard-opens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!