软键盘推布局 [英] Soft keyboard pushing layout
问题描述
我一直在尝试许多解决方案,以防止软键盘在布局出现时推入布局.但是,所有解决方案均无效.
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.
简而言之,当显示软键盘时,我希望布局中的所有内容(按钮,imageViews,背景,textViews,textEdits)都保持在原位.我不在乎它将覆盖什么.
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:
- (没有用)我尝试在我的
AndroidManifest.xml
中编写 - (没有用)我尝试在我的
onCreate()
上写getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
,尽管它没有用 - 我尝试编写
android:windowSoftInputMode ="adjustResize"
在我的AndroidManifest.xml
中也无法正常工作 - (没有用)我尝试在我的
AndroidManifest.xml
中编写 - (没有用),我还尝试在我的
AndroidManifest.xml
中编写
android:windowSoftInputMode ="adjustNothing"
android:windowSoftInputMode ="stateHidden | adjustPan"
android:windowSoftInputMode ="stateVisible | adjustPan"
- (did not work) I tried writing
android:windowSoftInputMode="adjustNothing"
in myAndroidManifest.xml
- (did not work) I tried writing
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
in myonCreate()
though it did not work - I tried writing
android:windowSoftInputMode="adjustResize"
in myAndroidManifest.xml
and also did not work - (did not work) I tried writing
android:windowSoftInputMode="stateHidden|adjustPan"
in myAndroidManifest.xml
- (did not work) I also tried writing
android:windowSoftInputMode="stateVisible|adjustPan"
in myAndroidManifest.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?
这是我的AndroidManifest.xml
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
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>
推荐答案
在xml文件中删除背景图片
Remove your Background image in xml file
使用以下代码在Java文件中设置背景图片:
Set Background image in java file using below code:
getWindow().setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.banner1));
这篇关于软键盘推布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!