【问题标题】:Android Toolbar moves up when keyboard appears出现键盘时Android工具栏向上移动
【发布时间】:2015-09-18 10:26:23
【问题描述】:

我正在创建一个基于聊天的 UI 屏幕,其中我有聊天消息的工具栏和回收器视图,以及回复消息布局。

每当edittext获得焦点时,它就会向上移动工具栏。相反,我想调整 recyclerview 的大小。

一些stackoverflow 答案建议在工具栏下方放置一个空滚动视图,但它不起作用。

        <activity
            android:name=".PostMessageActivity"
            android:label="@string/title_activity_post_message"
            android:windowSoftInputMode="stateVisible|adjustResize"
            >
        </activity>

我在清单文件中将windowSoftInputMode 设置为stateVisible|adjustPan

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.pasonet.yokibu.PostMessageActivity"
>
<include
    android:id="@+id/toolbar_home"
    layout="@layout/toolbar_home"
    android:elevation="5dp"
    />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ScrollView>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar_home"
    android:orientation="vertical"
    android:layout_above="@+id/add_post_layout"
    >
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/post_msg_recyclerview"
        >
    </android.support.v7.widget.RecyclerView>


</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:padding="5dp"
    android:id="@+id/add_post_layout"
    android:background="#ffffff"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:elevation="5dp"
    android:layout_margin="0pt"
   >

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/messageText"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:maxLines="4"
        android:scrollbars="vertical"
        android:background="@color/trasnperant"
        android:hint="Type your message"
        android:layout_marginBottom="4dp"
        />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_send_black_36dp"
        android:id="@+id/sendButton"
        android:background="@drawable/abc_btn_default_mtrl_shape"
        android:onClick="addPost"
        />
</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout android-toolbar android-recyclerview android-appcompat


    【解决方案1】:

    问题是我在styles.xml 中使用了&lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt;

    要启用adjustResize,请在活动的父布局中添加android:fitsSystemWindows="true"

    【讨论】:

    • 对我不起作用,一般情况下,当您在此屏幕内有 bg 时很难管理,只需调整 pan 而不调整背景大小。在背景和状态栏固定的情况下,这不起作用。我在这里搜索以解决此问题,但我还没有找到任何东西。
    • 添加此代码后,我的屏幕已完美调整,但我得到一个白色补丁,即我的工具栏中的边缘底部之类的东西。如何避免这种情况。?
    • 拯救了一周!谢谢
    • 此代码有效。但在工具栏下方和上方显示白色状态栏
    • 这不行,我用的是AppBarLayoutToolBarNestedScrollView
    【解决方案2】:

    或者在清单中添加这一行。 android:windowSoftInputMode="adjustResize"

    【讨论】:

    • 这是一个糟糕的解决方案,因为使用 adjustResize 您的页面与 textedit 将不会滚动到元素
    【解决方案3】:

    如果您不希望您的Toolbar 被推高,您不应该使用adjustPan。使用adjustResize 而不使用任何其他东西(不添加任何额外的Views)就足够了。

    【讨论】:

    • 感谢您的宝贵时间。我试过adjustResize 也没有用。我还缺少其他东西吗?
    • 因此您的布局一定有问题。老实说,我并没有真正得到您提供的布局文件。为什么你的RecyclerView 包裹在LinearLayout 中?此外,由于您决定使用RelativeLayout 作为根,请尝试为您的Toolbar 设置android:layout_alignParentTop="true
    【解决方案4】:

    在我的例子中,我的默认应用程序使用 Coordinator Layout 作为根元素。即使做上面描述的也不能解决我的问题。 然后我做了以下事情:

    1. 将根布局从 Coordinator Layout 更改为 RelativeLayout
    2. android:fitsSystemWindows="true" 包含在根布局中
    3. 在活动清单标签中包含android:windowSoftInputMode="adjustResize|stateAlwaysHidden"

    【讨论】:

    • 为我工作!谢谢!
    【解决方案5】:

    您应该在工具栏的根布局中添加 android:fitsSystemWindows="true"。如果将它添加到 Activity 的父布局中,则会在工具栏上方出现一条奇怪的线(带有状态栏高度)。

    【讨论】:

    • 谢谢!你让我开心!
    【解决方案6】:
    <include
        android:id="@+id/toolbar_home"
        layout="@layout/toolbar_home"
        android:elevation="5dp"
    android:layout_alignParentTop="true"
        />
    

    【讨论】:

      【解决方案7】:

      这是此修复的解决方案

      • android:windowSoftInputMode="adjustResize" 在清单文件中
      • 创建一个“CommentKeyBoardFix.Java”类并复制粘贴以下代码。

        public class CommentKeyBoardFix
        {
        private View mChildOfContent;
        private int usableHeightPrevious;
        private FrameLayout.LayoutParams frameLayoutParams;
        private Rect contentAreaOfWindowBounds = new Rect();
        
        public CommentKeyBoardFix(Activity activity)
        {
            FrameLayout content = activity.findViewById(android.R.id.content);
            mChildOfContent = content.getChildAt(0);
            mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
            frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
        }
        
        private void possiblyResizeChildOfContent()
        {
            int usableHeightNow = computeUsableHeight();
            if (usableHeightNow != usableHeightPrevious)
            {
                int heightDifference = 0;
                if (heightDifference > (usableHeightNow /4))
                {
                    // keyboard probably just became visible
                    frameLayoutParams.height = usableHeightNow - heightDifference;
                }
                else
                {
                    // keyboard probably just became hidden
                    frameLayoutParams.height = usableHeightNow;
                }
                mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
                mChildOfContent.requestLayout();
                usableHeightPrevious = usableHeightNow;
            }
        }
        
        private int computeUsableHeight()
        {
            mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
            return contentAreaOfWindowBounds.height();
        }
        }
        

        然后在你的Activity或者Fragment中调用这个类;对于 Kotlin:

        setContentView(R.layout.your_comments_layout)
        CommentKeyBoardFix(this) 
        

        或者对于 Java:

        new CommentKeyBoardFix(this)
        

      【讨论】:

      • 使用此修复程序时屏幕闪烁。
      【解决方案8】:

      请检查 Android Manifest 中的 WindowsSoftInputMode 尝试设置 AdjustResize 或 AdjsutPan

      【讨论】:

      • AdjustPan 是芝麻开门密码
      【解决方案9】:
      1. android:fitsSystemWindows="true" 包含到活动的xml 文件的根目录中。
      2. android:windowSoftInputMode="stateHidden|adjustResize" 包含到活动清单标记中。

      这两步解决了我的问题。

      【讨论】:

        【解决方案10】:

        试试这个,

        android:windowSoftInputMode="adjustPan"
        

        在您的 manifest.xml 中的活动标记中。

        【讨论】:

          【解决方案11】:

          在清单中的活动上使用android:windowSoftInputMode="adjustNothing" 对我有用。

          【讨论】:

            【解决方案12】:

            我尝试了很多方法来保持工具栏固定,而不是通过 android:windowSoftInputMode="adjustResize" 调整或压缩布局,这是不合适的。

            这只能通过协调器布局实现,

            <android.support.design.widget.CoordinatorLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            
            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            
                <android.support.design.widget.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
            
                    <android.support.v7.widget.Toolbar 
                       xmlns:android="http://schemas.android.com/apk/res/android"
                       xmlns:ripple="http://schemas.android.com/apk/res-auto"
                       android:id="@+id/tv_toolbar"
                       android:layout_width="match_parent"
                       android:layout_height="50dp"
                       android:background="#fff"
                       ripple:contentInsetStart="0dp">
                    </android.support.v7.widget.Toolbar>
            
                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>
            
            <ScrollView
                android:id="@+id/coordinate_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:marginTop="56dp">
            </ScrollView>
            

            保持结构为

             Coordinator layout
              |-->Toolbar    (android:id="@+id/toolbar")
              |-->ScrollView (android:layout_below="@id/toolbar")
               |-->Child
               |-->Child
               |-->Child
            

            【讨论】:

              【解决方案13】:

              如果 NOTHING 起作用,请注意库可以操纵软输入,例如 com.arlib.floatingsearchview

              发生的情况是,例如,您有带有搜索视图的列表视图,当您显示带有floatingsearchview 的片段时,其设置的软输入为SOFT_INPUT_ADJUST_PAN(您可能不关心带有列表视图的片段),但是如果下一个片段将是带有许多编辑文本的项目详细信息,它将向上推工具栏。

              在我的情况下,我无法替换这个库,所以我在基本片段中设置了 SOFT_INPUT_ADJUST_RESIZE

              @Override
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
              }
              

              【讨论】:

                【解决方案14】:

                就我而言,我必须将styles.xml 中的windowFullscreenabove answer 结合使用才能使其工作

                <item name="android:windowFullscreen">false</item>
                

                【讨论】:

                  【解决方案15】:

                  我添加了这个

                  适合系统窗口

                  在滚动视图中,它解决了:

                  <ScrollView
                          android:id="@+id/rootScrollview"
                          android:layout_width="0dp"
                          android:layout_height="0dp"
                          android:fitsSystemWindows="true">
                  

                  【讨论】:

                    【解决方案16】:

                    我使用CoordinatorLayout 作为工具栏的根布局。我有同样的问题,但通过将工具栏设置为:

                    app:layout_scrollFlags="enterAlways" 
                    

                    代替:

                    app:layout_scrollFlags="scroll|enterAlways"
                    

                    并添加:

                    android:windowSoftInputMode="adjustNothing"
                    

                    在清单中的活动中。

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2012-08-13
                      • 1970-01-01
                      • 2018-08-21
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2016-12-20
                      相关资源
                      最近更新 更多