【问题标题】:How to move up the whole layout when keyboard shown?显示键盘时如何向上移动整个布局?
【发布时间】:2014-12-17 15:05:37
【问题描述】:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_gravity="bottom"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" /> 
        </LinearLayout>    

</FrameLayout>  

在清单中:

 android:windowSoftInputMode="adjustPan"

初始行为:

当前行为:

期望的行为:

如何实现所需的布局行为? adjustResize 不合适,因为此选项会调整布局大小。

感谢您的回答!

【问题讨论】:

  • 我认为你应该使用滚动视图作为主要的父布局
  • 对不起,我不明白你的意图。假设在文本视图的上方和下方还有另外两个按钮;您希望的行为是什么?如果一切都向上移动,那怎么不是adjustResize?

标签: android android-softkeyboard


【解决方案1】:

获得该结果的唯一方法是调整布局大小。 使用 android:windowSoftInputMode="adjustResize"

【讨论】:

  • adjustResize 不适合我,因为此选项会调整布局大小
【解决方案2】:

您可能想看看this answer。来自它的建议:

将Window SoftInput Mode属性设置为adjustPan和adjustResize

<activity android:windowSoftInputMode="adjustPan|adjustResize"> </activity>

【讨论】:

    【解决方案3】:

    在你的清单中试试这个:

    android:windowSoftInputMode="adjustResize|stateUnchanged"
    

    【讨论】:

      【解决方案4】:

      检查包含EditText和Button的布局是否为RelativeLayout。

      【讨论】:

      • 尝试将LinearLayout替换为RelativeLayout
      • 尝试将框架布局替换为相对布局
      【解决方案5】:

      尝试在您的LinearLayout 中使用alignParentBottom="true" 属性将FrameLayout 更改为RelativeLayout

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:orientation="vertical"
          android:layout_height="match_parent">
      
          <LinearLayout
              android:layout_alignParentBottom="true"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" /> 
              </LinearLayout>    
      
      </RelativeLayout>  
      

      【讨论】:

        【解决方案6】:

        您将无法使用 adjustPan 来实现它,因为来自 official guide

        Activity 的主窗口没有调整大小来为软键盘腾出空间。相反,窗口的内容会自动平移,因此当前焦点永远不会被键盘遮挡,用户始终可以看到他们正在输入的内容。这通常不如调整大小可取,因为用户可能需要关闭软键盘才能到达窗口的模糊部分并与之交互。

        您的Button 低于您当前关注的EditText

        【讨论】:

          【解决方案7】:

          通过从活动中删除以下行并从清单中删除 android:windowSoftInputMode,我得到了以前被隐藏的整个布局滚动

          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

          请查看此输出视频链接: https://mega.nz/#!TpRCWZjY!TqDzv0Yn3Q24PR1JAdH8kRVeDf2iSa1LQbNBgF0SVHk

          【讨论】:

            【解决方案8】:

            我不相信有一种简单的方法可以做到这一点。至少不像设置标志那么简单。 我有一个类似的情况,希望键盘上升到远低于所选 EditText 的视图级别。我最终使用了这个答案: How to check visibility of software keyboard in Android?

            并在键盘可见时手动调整我的视图大小。

            【讨论】:

              【解决方案9】:

              只需在活动中使用此属性

              android:windowSoftInputMode="adjustPan"
              

              【讨论】:

                猜你喜欢
                • 2010-12-30
                • 2012-02-25
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-11-01
                • 1970-01-01
                • 2019-03-05
                相关资源
                最近更新 更多