Soft keyboard pushing layout

南笙酒味 提交于 2021-01-28 02:29:27

问题


I have been trying many solutions out there for preventing the soft keyboard from pushing my layout when it appears. However, all solutions did not work.

Simply, I want everything in my layout (buttons, imageViews, Background, textViews, textEdits) to stay in place when soft keyboard is shown. I do not care about what it will cover.

I tried all of the following and all produced the same output and do not work:

  1. (did not work) I tried writing android:windowSoftInputMode="adjustNothing" in my AndroidManifest.xml
  2. (did not work) I tried writing getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);in myonCreate() though it did not work
  3. I tried writing android:windowSoftInputMode="adjustResize" in my AndroidManifest.xml and also did not work
  4. (did not work) I tried writing android:windowSoftInputMode="stateHidden|adjustPan" in my AndroidManifest.xml
  5. (did not work) I also tried writing android:windowSoftInputMode="stateVisible|adjustPan" in my AndroidManifest.xml

All the above solutions produced the following snapshot:

The actual layout looks like this (this is what I want it to keep looking when soft keyboard is active):

Any idea of how to prevent this?

This is my AndroidManifest.xml

<?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="xxxxxxx">
    <!-- Include required permissions for Google Mobile Ads to run. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.VIBRATE"/>

    <supports-screens
        android:xlargeScreens="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        tools:replace="android:supportsRtl"
        android:theme="@style/AppTheme"> <!-- This meta-data tag is required to use Google Play Services. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MainActivity"
            android:configChanges="orientation"
            android:screenOrientation="portrait"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> <!-- Include the AdActivity configChanges and theme. -->
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:windowSoftInputMode="stateVisible|adjustPan"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>

MainActivity.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"


    <!-- view for AdMob Banner Ad -->


    <!--Real Ad ads:adUnitId="@string/banner_ad_unit_id" -->

    <ImageView
        android:id="@+id/background"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:scaleType="fitXY"
        android:visibility="visible"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toLeftOf="@id/relativeLayout"
        ads:layout_constraintRight_toRightOf="@id/relativeLayout"
        ads:layout_constraintTop_toTopOf="parent"
        ads:layout_constraintVertical_bias="0.0" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:visibility="visible"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-5739450654089125/9466012825"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"
        ads:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/submitScore"
        android:layout_width="88dp"
        android:layout_height="48dp"
        android:text="@string/submit"
        ads:layout_constraintBottom_toBottomOf="@id/ourScoreTextInput"
        ads:layout_constraintRight_toRightOf="@id/adView" />

    <EditText
        android:id="@+id/theirScoreTextInput"
        android:layout_width="95dp"
        android:layout_height="42dp"
        android:layout_marginTop="4dp"
        android:ems="10"
        android:inputType="number"
        android:scrollbars="vertical"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        android:visibility="visible"
        ads:layout_constraintBottom_toBottomOf="@id/ourScoreTextInput"
        ads:layout_constraintLeft_toLeftOf="@id/adView"
        ads:layout_constraintTop_toBottomOf="@id/theirTextFieldDescription" />

    <EditText
        android:id="@+id/ourScoreTextInput"
        android:layout_width="95dp"
        android:layout_height="0dp"
        android:layout_marginBottom="389dp"
        android:ems="10"
        android:inputType="number"
        android:scrollbars="vertical"
        android:textColor="@android:color/background_light"
        android:textStyle="bold"
        android:visibility="visible"
        ads:layout_constraintBaseline_toBaselineOf="@id/theirScoreTextInput"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toRightOf="@id/theirScoreTextInput"
        ads:layout_constraintRight_toLeftOf="@id/submitScore"
        ads:layout_constraintTop_toBottomOf="@id/ourTextFieldDescription" />

    <TextView
        android:id="@+id/theirTextFieldDescription"
        android:layout_width="wrap_content"
        android:layout_height="27dp"
        android:text="@string/their_score"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="20sp"
        ads:layout_constraintBottom_toTopOf="@+id/theirScoreTextInput"
        ads:layout_constraintHorizontal_bias="0.472"
        ads:layout_constraintLeft_toLeftOf="@+id/theirScoreTextInput"
        ads:layout_constraintRight_toRightOf="@id/theirScoreTextInput"
        ads:layout_constraintTop_toBottomOf="@+id/adView"
        ads:layout_constraintVertical_bias="0.283" />

    <TextView
        android:id="@+id/ourTextFieldDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/our_score"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="20sp"
        ads:layout_constraintBaseline_toBaselineOf="@id/theirTextFieldDescription"
        ads:layout_constraintBottom_toBottomOf="@id/ourTextFieldDescription"
        ads:layout_constraintBottom_toTopOf="@id/ourScoreTextInput"
        ads:layout_constraintLeft_toLeftOf="@id/ourScoreTextInput"
        ads:layout_constraintRight_toRightOf="@+id/ourScoreTextInput"
        ads:layout_constraintTop_toTopOf="@id/theirTextFieldDescription" />

    <TextView
        android:id="@+id/theirScoreSheet"
        android:layout_width="150dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="8dp"
        android:background="#1FFF"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="24sp"
        android:textStyle="bold"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintLeft_toLeftOf="@id/background"
        ads:layout_constraintRight_toLeftOf="@id/frameLayout"
        ads:layout_constraintTop_toBottomOf="@+id/theirScoreSheetDescription"
        ads:layout_goneMarginLeft="16dp"
        ads:layout_goneMarginRight="16dp" />

    <TextView
        android:id="@+id/ourScoreSheet"
        android:layout_width="147dp"
        android:layout_height="0dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="#1FFF"
        android:scrollbars="vertical"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="24sp"
        android:textStyle="bold"
        ads:layout_constraintBottom_toBottomOf="@+id/theirScoreSheet"
        ads:layout_constraintLeft_toRightOf="@id/frameLayout"
        ads:layout_constraintRight_toRightOf="@id/background"
        ads:layout_constraintTop_toTopOf="@id/theirScoreSheet"
        ads:layout_constraintVertical_bias="0.0"
        ads:layout_goneMarginLeft="16dp"
        ads:layout_goneMarginRight="16dp" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="5dp"
        android:layout_height="0dp"
        ads:layout_constraintBottom_toBottomOf="@+id/background"
        ads:layout_constraintEnd_toEndOf="parent"
        ads:layout_constraintHorizontal_bias="0.5"
        ads:layout_constraintStart_toStartOf="parent"
        ads:layout_constraintTop_toTopOf="parent"
        ads:layout_constraintVertical_bias="0.0">

    </FrameLayout>

    <ImageView
        android:id="@+id/gear"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/gear"
        android:contentDescription="@string/gear"
        android:visibility="visible"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:srcCompat="@drawable/gear" />

    <TextView
        android:id="@+id/theirScoreSheetDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="@string/their_score"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="22sp"
        android:textStyle="bold"
        ads:layout_constraintLeft_toLeftOf="@id/theirScoreSheet"
        ads:layout_constraintRight_toRightOf="@id/theirScoreSheet"
        ads:layout_constraintTop_toBottomOf="@id/theirScoreTextInput" />

    <TextView
        android:id="@+id/ourScoreTotalLarge"
        android:layout_width="138dp"
        android:layout_height="wrap_content"
        android:alpha="0.4"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="75sp"
        android:textStyle="bold"
        ads:layout_constraintBottom_toBottomOf="@+id/ourScoreSheet"
        ads:layout_constraintLeft_toLeftOf="@+id/ourScoreSheet"
        ads:layout_constraintRight_toRightOf="@+id/ourScoreSheet"
        ads:layout_constraintTop_toTopOf="@+id/ourScoreSheet" />

    <TextView
        android:id="@+id/theirScoreTotalLarge"
        android:layout_width="138dp"
        android:layout_height="wrap_content"
        android:alpha="0.4"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="75sp"
        android:textStyle="bold"
        ads:layout_constraintBottom_toBottomOf="@+id/ourScoreTotalLarge"
        ads:layout_constraintLeft_toLeftOf="@+id/theirScoreSheet"
        ads:layout_constraintRight_toRightOf="@+id/theirScoreSheet"
        ads:layout_constraintTop_toTopOf="@id/ourScoreTotalLarge" />

    <TextView
        android:id="@+id/ourScoreSheetDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/our_score"
        android:textAlignment="center"
        android:textColor="@android:color/background_light"
        android:textSize="22sp"
        android:textStyle="bold"
        ads:layout_constraintBottom_toBottomOf="@+id/theirScoreSheetDescription"
        ads:layout_constraintLeft_toLeftOf="@+id/ourScoreSheet"
        ads:layout_constraintRight_toRightOf="@+id/ourScoreSheet"
        ads:layout_constraintTop_toTopOf="@+id/theirScoreSheetDescription" />

    <Button
        android:id="@+id/settingsButton"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:alpha="0.0"
        ads:layout_constraintBottom_toBottomOf="@+id/gear"
        ads:layout_constraintLeft_toLeftOf="@+id/gear"
        ads:layout_constraintRight_toRightOf="@+id/gear"
        ads:layout_constraintTop_toTopOf="@+id/gear" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        ads:layout_constraintGuide_end="364dp" />
</android.support.constraint.ConstraintLayout>

回答1:


Remove your Background image in xml file

Set Background image in java file using below code:

getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.banner1));



回答2:


This work for me

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



回答3:


I recently encountered the same thing. While staying in one Activity, sometimes the soft keyboard would cause re-layouts and sometimes it wouldn't, depending on what Fragment I had loaded. Finally I figured it out. It was caused by a RecyclerView being visible:

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="1dp"
        android:layout_height="1dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:visibility="visible"/>

That's it. If the Fragment I had loaded had a visible RecyclerView, the soft keyboard would cause re-layout. If not, it wouldn't. It even causes a re-layout of stuff that isn't part of the Fragment that contains the RecyclerView.

That's how it looks in a ConstraintLayout. But it could also be in a RelativeLayout. If its visibility is "gone", the soft keyboard's appearance doesn't cause re-layout. If it's "visible", the soft keyboard's appearance causes a re-layout of the whole Activity. I don't know what versions of Android this "works" on, and I don't know whether it's intentional.

I know you don't have one, but maybe something else in your .xml does the same thing, or has a RecyclerView in it. I'd surmise that the presence of the RecyclerView causes the SoftInputMode to be set. You could consider waiting for your view to finish laying out, then changing SoftInputMode, and see if it sticks.



来源:https://stackoverflow.com/questions/49378136/soft-keyboard-pushing-layout

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