Android 键盘出现时工具栏上移

Android Toolbar moves up when keyboard appears

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

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

一些 Whosebug 答案 建议在 工具栏 下方放置一个 空滚动视图 ,但是没用。

        <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>

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

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

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

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

试试这个,

android:windowSoftInputMode="adjustPan"

在 activity 标签中的 manifest.xml。

问题是我在 styles.xml

中使用 <item name="android:windowTranslucentStatus">true</item>

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

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

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

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

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

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

解决了它
app:layout_scrollFlags="enterAlways" 

而不是:

app:layout_scrollFlags="scroll|enterAlways"

并添加:

android:windowSoftInputMode="adjustNothing"

在清单中 activity。

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

只有 Coordinator 布局才有可能,

<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

这是此修复的解决方案

  • android:windowSoftInputMode="adjustResize" 清单文件
  • 制作一个class"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 或片段中调用此 class;对于科特林:

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

    或 Java:

    new CommentKeyBoardFix(this)
    

如果 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);
}

在我的例子中,我不得不在 styles.xml 中将 windowFullscreen 设置为 false 并结合 使其工作

<item name="android:windowFullscreen">false</item>
  1. android:fitsSystemWindows="true" 添加到活动 xml 文件的根目录。
  2. android:windowSoftInputMode="stateHidden|adjustResize" 添加到 activity 清单标签。

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

我添加了这个

fitsSystemWindows

在 scrollview 里面解决了:

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

我最初在 activity 的 AndroidManifest 上有 android:windowSoftInputMode="adjustPan",底部有工具栏、聊天消息回收器视图和输入。

这是在输入视图接收焦点和键盘打开时回收器视图上升但工具栏隐藏的预期结果

在尝试了一系列解决方案后,对我有用的是: 根本没有windowSoftInputMode

app:stackFromEnd="true" 在回收者视图中