软键盘推送布局

时间:2018-03-20 07:04:34

标签: java android layout view keyboard

我一直在尝试许多解决方案,以防止软键盘出现时推动我的布局。但是,所有解决方案都不起作用。

简单地说,我希望在显示软键盘时,我的布局中的所有内容(按钮,imageViews,背景,textViews,textEdits)都保持不变。我不关心它会涵盖什么。

我尝试了以下所有内容并且都生成了相同的输出并且不起作用:

  1. (没用)我尝试在android:windowSoftInputMode="adjustNothing"
  2. 中写AndroidManifest.xml
  3. (没有用)我尝试在我的getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);中写onCreate()虽然它不起作用
  4. 我尝试写android:windowSoftInputMode="adjustResize" 在我的AndroidManifest.xml中也没有工作
  5. (没用)我尝试在android:windowSoftInputMode="stateHidden|adjustPan"
  6. 中写AndroidManifest.xml
  7. (没用)我也尝试在android:windowSoftInputMode="stateVisible|adjustPan"
  8. 中写AndroidManifest.xml

    以上所有解决方案都产生了以下快照:

    实际布局看起来像这样(这是我希望它在软键盘处于活动状态时继续查看):

    有任何关于如何防止这种情况的想法吗?

    这是我的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>
    

3 个答案:

答案 0 :(得分:1)

删除xml文件中的背景图像

使用以下代码在java文件中设置背景图像:

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

答案 1 :(得分:0)

这项工作对我来说

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

答案 2 :(得分:0)

我最近遇到了同样的事情。在进行一项“活动”时,有时软键盘会导致重新布局,有时却不会,这取决于我加载的片段。终于我明白了。这是由于可见的RecyclerView造成的:

    <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"/>

就是这样。如果我加载的片段具有可见的RecyclerView,则软键盘将导致重新布局。如果没有,那就不会。甚至会重新布局不属于RecyclerView的Fragment的内容。

这就是ConstraintLayout中的外观。但是它也可以在RelativeLayout中。如果可见性已消失,则软键盘的外观不会引起重新布局。如果它是“可见的”,则软键盘的外观将导致整个活动的重新布局。我不知道它可以在哪个版本的Android上运行,也不知道它是否是故意的。

我知道您没有,但是.xml中的其他内容可能会做同样的事情,或者其中包含RecyclerView。我猜想RecyclerView的存在会导致设置SoftInputMode。您可以考虑等待视图完成布局,然后更改SoftInputMode并查看是否粘住。