键盘弹出时屏幕布局上移

23

假设我有以下布局:

enter image description here

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

enter image description here

正如您所注意到的,上部的图片向上移动,整个布局也随之移动。 这是此布局的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>

我还在清单文件中为该活动设置了这个:

android:windowSoftInputMode="adjustPan"

请问是否有方法可以在键盘弹出时停止我的布局移动? 谢谢!!!


请查看此链接:https://dev59.com/eGkw5IYBdhLWcg3wUIxW - Yogesh Somani
我认为这个链接会对你有所帮助。链接谢谢... - user4232
@YogeshSomani 我已经尝试了所有方法,但仍然无法正常工作。 - adrian
好的,我在我的应用程序清单中没有提到任何“windowSoftInputMode”,它运行得很好。当键盘打开时,屏幕不会上移。尝试这种方法 - 只需从您的清单中删除“windowSoftInputMode”参数即可。 - Yogesh Somani
@YogeshSomani 它有效。将其发布为答案,接受并点赞它。 - adrian
6个回答

32

尝试使用

android:windowSoftInputMode="adjustPan|adjustResize"

为了这项活动。


5
尝试在你的ScrollView中添加 android:isScrollContainer="false",这可能会有帮助。 - SALMAN
尝试使用以下代码 android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"。 - SALMAN
不要同时使用adjustPan和adjustResize,只能选择其中一个。 - CoolMind
@CoolMind为什么我们不应该同时使用它们? - D4rWiNS
1
@D4rWiNS,抱歉,我不记得了。已经测试过了,它们可以同时工作,尽管adjustPan就足够了。如果我没记错的话,问题可能出在带有背景图片的布局上 - 它也被调整大小了。但我可能记错了。 - CoolMind
虽然这段代码片段很受欢迎,可能会提供一些帮助,但如果它包括解释“如何”和“为什么”解决问题,那将会大有改进。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 - Toby Speight

12

嗯,我的应用程序清单中没有提到任何"windowSoftInputMode",但它可以正常运行。当键盘打开时,屏幕不会向上移动。尝试这种方法-只需从您的清单中删除"windowSoftInputMode"参数。


3

试试这个:

android:windowSoftInputMode="adjustNothing"

1
虽然这段代码片段很受欢迎,可能会提供一些帮助,但如果它包括解释“如何”和“为什么”解决问题,那将会大有改进。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人!请编辑您的答案以添加解释,并指出适用的限制和假设。 - Toby Speight

2

我的EditText也一直在向上移动。

将重力添加到EditText中似乎可以解决该问题。 键盘一直在将我的EditText向上推,所以我在XML中将EditText的重力添加到底部。

android:gravity=bottom

1

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

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

0

我已经设置了windowsoftInputMode以防止键盘弹出,只需添加注释代码

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接