【问题标题】:layout of the screen moves up when keyboard is displayed显示键盘时屏幕布局向上移动
【发布时间】:2012-07-10 09:21:42
【问题描述】:

假设我有以下布局:

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

您可以注意到顶部的图像向上移动,整个布局移动。 这是我的这个布局的 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"

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

【问题讨论】:

  • 我认为这个链接会对你有所帮助。 [链接][1] [1]:stackoverflow.com/questions/5516216/… 谢谢...
  • @YogeshSomani 里面没有我没试过的东西。它不工作
  • 好吧,我没有在我的应用程序清单中提到任何“windowSoftInputMode”,它运行良好。键盘打开时屏幕不向上移动。试试这种方法 - 只需从清单中删除“windowSoftInputMode”参数。
  • @YogeshSomani 它有效。将其发布为答案,接受并点赞

标签: android layout keyboard


【解决方案1】:

尝试使用

android:windowSoftInputMode="adjustPan|adjustResize"

活动。

【讨论】:

  • 为你的 ScrollView 试试这个 android:isScrollContainer="false" 这可能对你有帮助。
  • 也试试这个 android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
  • 不要同时使用adjustPan和adjustResize,只能使用其中之一。
  • @CoolMind 为什么我们不应该同时使用它们?
  • @D4rWiNS,对不起,我不记得了。已经对其进行了测试,并且它们同时工作,尽管 adjustPan 就足够了。如果我没有忘记,问题出在带有背景图片的布局中——它也被调整了大小。但我可能弄错了。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    试试这个:

    android:windowSoftInputMode="adjustNothing"
    

    【讨论】:

    • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of howwhy 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
    【解决方案4】:

    我的 EditText 也在不断上升。

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

    android:gravity=bottom
    

    【讨论】:

      【解决方案5】:

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

      android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 2010-12-30
          • 2016-02-04
          • 2012-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-20
          • 1970-01-01
          相关资源
          最近更新 更多