当键盘出现时,Android工具栏会向上移动。

40

我正在创建一个基于聊天的UI屏幕,其中包括聊天消息的工具栏、recyclerview和回复消息布局。

每当编辑文本框获得焦点时,它会将工具栏向上移动。相反,我想调整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>

这里输入图片描述

17个回答

21
问题在于我在styles.xml中使用了<item name="android:windowTranslucentStatus">true</item>。要启用adjustResize,请在您的活动父布局中添加android:fitsSystemWindows="true"

2
对我来说不起作用。通常当您在此屏幕内有一个背景时,很难管理,只需调整Pan而不是调整背景大小。在固定背景和状态栏的情况下,这并不起作用。我正在这里寻找解决方法,但我还没有找到任何东西。 - luizfelipetx
添加了这段代码后,我的屏幕调整得非常完美,但是我在我的工具栏中出现了一个白色补丁,就像底部边距一样。如何避免这种情况? - Savin Sharma
挽救了这个星期!谢谢。 - Eu Vid
这个不起作用,我正在使用 AppBarLayoutToolBarNestedScrollView - Konstantin Konopko
使用 adjustResize,当键盘弹出时会失去动画效果,这是一个不好的选择。 - famfamfam
显示剩余2条评论

14

或者在清单文件中添加以下代码:android:windowSoftInputMode="adjustResize"


4
这种解决方案不好,因为使用 adjustResize 时,使用 textedit 的页面无法滚动到元素位置。 - Konstantin Konopko

11

如果您不想将工具栏推上去,那么您不应该使用adjustPan。仅使用adjustResize(不添加任何其他Views)就足够了。


谢谢您的时间。我已经尝试了adjustResize,但它仍然没有起作用。我还有什么遗漏吗? - AlgoCoder
因此,您的布局肯定存在一些问题。说实话,我并不是很理解您提供的布局文件。为什么您的RecyclerView被包裹在一个LinearLayout中?另外,既然您决定将RelativeLayout作为根布局,请尝试为您的Toolbar设置android:layout_alignParentTop="true" - Bartek Lipinski

9

在我的情况下,我的默认应用程序使用协调布局作为根元素。即使按照上述所描述的方式操作,也无法解决我的问题。

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

可以用!谢谢! - elirigobeli

5

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


谢谢!你让我的一天变得更美好了! - alvarlagerlof

3
最初的回答:
这是解决此问题的方法。
翻译后的内容:
以下是解决此问题的方法:

输入图片描述

  • android:windowSoftInputMode="adjustResize" in Manifest File
  • Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

    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();
    }
    }
    

    And then call this class in your Activity or Fragment; for Kotlin:

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

    or for Java:

    new CommentKeyBoardFix(this)
    

2
使用这个修复方法时屏幕会闪烁。 - SRBhagwat

2
<include
    android:id="@+id/toolbar_home"
    layout="@layout/toolbar_home"
    android:elevation="5dp"
android:layout_alignParentTop="true"
    />

1
请查看Android清单中的WindowsSoftInputMode,尝试设置AdjustResize或AdjsutPan,保留HTML标签。

"AdjustPan" 是打开密码的魔法词语。 - fullmoon

1
  1. 在活动的xml文件中的根标签中加入android:fitsSystemWindows="true"
  2. 在活动的清单文件中的活动标签中加入android:windowSoftInputMode="stateHidden|adjustResize"

这两个步骤解决了我的问题。


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

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