[英]Android Toolbar moves up when keyboard appears
我正在创建一个基于聊天的 UI 屏幕,其中有用于聊天消息的工具栏和回收站视图,以及回复 msg 布局。
每当 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>
问题是,我使用<item name="android:windowTranslucentStatus">true</item>
在styles.xml
要启用adjustResize
在您的活动的父布局中添加android:fitsSystemWindows="true"
或者在清单中添加这一行。 android:windowSoftInputMode="adjustResize"
如果您不想向上推Toolbar
,则不应使用adjustPan
。 使用没有任何其他东西的adjustResize
(没有添加任何额外的Views
)应该就足够了。
就我而言,我的默认应用程序使用 Coordinator Layout 作为根元素。 即使执行上述操作也不能解决我的问题。 然后我做了以下事情:
android:fitsSystemWindows="true"
到根布局android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
到活动清单标签。您应该在工具栏的根布局中添加 android:fitsSystemWindows="true"。 如果将它添加到活动的父布局中,工具栏上方将出现一条奇怪的线(带有状态栏高度)。
这是此修复程序的解决方案
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 中调用这个类; 对于科特林:
setContentView(R.layout.your_comments_layout) CommentKeyBoardFix(this)
或对于 Java:
new CommentKeyBoardFix(this)
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home"
android:elevation="5dp"
android:layout_alignParentTop="true"
/>
请检查 Android Manifest 中的 WindowsSoftInputMode 尝试设置 AdjustResize 或 AdjsutPan
android:fitsSystemWindows="true"
包含在活动的 xml 文件中的根目录中。android:windowSoftInputMode="stateHidden|adjustResize"
到活动清单标记中。这两个步骤解决了我的问题。
试试这个,
android:windowSoftInputMode="adjustPan"
在活动标记中的 manifest.xml 中。
在清单中的活动上使用android:windowSoftInputMode="adjustNothing"
对我android:windowSoftInputMode="adjustNothing"
。
我尝试了很多方法来保持工具栏固定,同时不通过 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
如果没有任何效果,请注意库可以操作软输入,例如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>
我使用CoordinatorLayout
作为工具栏的根布局。 我遇到了同样的问题,但通过将工具栏设置为:
app:layout_scrollFlags="enterAlways"
代替:
app:layout_scrollFlags="scroll|enterAlways"
并添加:
android:windowSoftInputMode="adjustNothing"
在清单中的活动中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.