Android 显示键盘时屏幕布局向上移动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11410257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:18:35  来源:igfitidea点击:

layout of the screen moves up when keyboard is displayed

androidlayoutkeyboard

提问by adrian

Let say I have the following layout:

假设我有以下布局:

enter image description here

在此处输入图片说明

when I click on the edit text at the bottom I get this:

当我点击底部的编辑文本时,我得到了这个:

enter image description here

在此处输入图片说明

As you can notice the image from the top goes upper and the whole layout moves. This is my xml for this layout:

正如您所看到的,顶部的图像向上移动,整个布局移动。这是我用于此布局的 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#FFFFFF" >

   <LinearLayout
       android:id="@+id/header"
       android:layout_width="fill_parent"
       android:layout_height="75dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"        
       android:isScrollContainer="true"
       android:background="@drawable/header" >
   </LinearLayout>

   <ScrollView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/scrollauthentication"
       android:layout_below="@+id/header"
       android:background="#FFFFFF"
       android:fillViewport="true" >

       <RelativeLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:background="#FFFFFF" 
           android:id="@+id/authenticationrelativelayout">

           <TextView
               android:id="@+id/login"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_below="@+id/header"
               android:layout_marginLeft="30dp"
               android:layout_marginTop="20dp"
               android:text="login"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/lighter_orange"
               android:textSize="28dp" />

           <TextView
               android:id="@+id/usernameTextView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/login"
               android:layout_below="@+id/login"
               android:layout_marginTop="16dp"
               android:text="username"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />

           <EditText
               android:id="@+id/user"
               android:layout_width="260dp"
               android:layout_height="42dp"
               android:layout_alignLeft="@+id/usernameTextView"
               android:layout_below="@+id/usernameTextView"
               android:background="@drawable/edittext_selector"
               android:imeOptions="actionDone"
               android:lines="1"
               android:paddingLeft="10dp" />

           <TextView
               android:id="@+id/passwordTextView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/user"
               android:layout_below="@+id/user"
               android:text="password"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />

           <EditText
               android:id="@+id/password"
               android:layout_width="260dp"
               android:layout_height="42dp"
               android:layout_alignLeft="@+id/passwordTextView"
               android:layout_below="@+id/passwordTextView"
               android:background="@drawable/edittext_selector"
               android:imeOptions="actionDone"
               android:inputType="textPassword"
               android:lines="1"
               android:paddingLeft="10dp" >

               <requestFocus />
           </EditText>

           <CheckBox
               android:id="@+id/remembercheckBox"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/password"
               android:layout_below="@+id/password"
               android:layout_marginTop="37dp"
               android:button="@drawable/checkbox_selector"
               android:focusable="true" />

           <TextView
               android:id="@+id/rememberText"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignBaseline="@+id/remembercheckBox"
               android:layout_alignBottom="@+id/remembercheckBox"
               android:layout_toRightOf="@+id/remembercheckBox"
               android:text="Remember me"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />
       </RelativeLayout>
   </ScrollView>

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="30dp"
       android:layout_below="@+id/scrollauthentication"
       android:orientation="horizontal" >

       <Button
           android:id="@+id/loginButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@color/button_selected"
           android:text="log in"
           android:textColor="@drawable/textblack_selected"
           android:textStyle="bold" >
       </Button>

       <Button
           android:id="@+id/forgotten"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:background="@color/button_selected"
           android:text="forgotten password"
           android:textColor="@drawable/textblack_selected"
           android:textStyle="bold" />
   </LinearLayout>

</RelativeLayout>

And I have also set this in manifest file for this activity:

我还在此活动的清单文件中设置了它:

android:windowSoftInputMode="adjustPan"

So is there a way to stop my layout from moving when the keyboard is displayed? Thank you!!!!

那么有没有办法在显示键盘时阻止我的布局移动?谢谢!!!!

采纳答案by Yogesh Somani

well, I have not mentioned any "windowSoftInputMode" in my app's manifest and it runs fine. Screen does not move up when the keyboard opens up. Try this approach - just remove the "windowSoftInputMode" parameter from your manifest .

好吧,我没有在我的应用程序清单中提到任何“windowSoftInputMode”,它运行良好。键盘打开时屏幕不向上移动。试试这个方法 - 只需从清单中删除“windowSoftInputMode”参数。

回答by SALMAN

Try using

尝试使用

android:windowSoftInputMode="adjustPan|adjustResize"

for the activity.

为活动。

回答by Roberto Gutierrez

My EditText kept moving up as well.

我的 EditText 也一直向上移动。

Adding gravity to the Edit Text seems to solve the problem. The Keyboard was pushing my EditText up, so I added gravity to the bottom of the Edit Text in the XML.

将重力添加到“编辑文本”似乎可以解决问题。键盘将我的 EditText 向上推,因此我将重力添加到 XML 中 Edit Text 的底部。

android:gravity=bottom

回答by Rahul

Try this:

尝试这个:

android:windowSoftInputMode="adjustNothing"

回答by Charlie

I already had a windowsoftInputMode to prevent the keyboard from opening, so just add the comment code

我已经有一个windowsoftInputMode来防止键盘打开,所以只需添加注释代码

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

回答by Victor Sam VS

Try this, for me it worked...

试试这个,对我来说它有效......

<CoordinatorLayout
android:fitsSystemWindows="true">
<AppBarLayout>
    <Toobar>
        <!--...-->
    </Toobar>
</AppBarLayout>
<include layout="@layout/content_main_message" /> <!--EditText here-->
</CoordinatorLayout>