我的活动中有2个部分。一个是静态的,有一些数据,另一个是带有可滚动列表视图的聊天部分。当我从EditText
中type_message_area
输入数据并且listview
的尺寸增加时,.listview会推动软键盘后面的type_message_area
。如何修复type_message_area
控件以不在键盘后面,listview总是向上滚动。
<LinearLayout
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"
android:background="#f1f1f1"
android:orientation="vertical"
tools:context="com.myapp.application.ConversationActivity">
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<RelativeLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:orientation="horizontal"
tools:ignore="UselessParent">
<ImageView
android:id="@+id/imgUserProfile"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="25dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="15dp"
android:contentDescription="userProfileImage"/>
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="15dp"
android:layout_marginLeft="15dp"
android:layout_toEndOf="@id/imgUserProfile"
android:layout_toRightOf="@id/imgUserProfile"
android:ellipsize="marquee"
android:ems="12"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/primary_text_light"
android:textSize="18sp"
android:textStyle="normal"/>
<View android:id="@+id/separator"
android:background="@color/icon_gray"
android:layout_below="@id/nameTextView"
android:layout_width = "fill_parent"
android:layout_marginTop="4dp"
android:layout_height="2dip"/>
</RelativeLayout>
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<RelativeLayout
android:id="@+id/fullscreen_content"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal"
android:orientation="horizontal"
tools:ignore="UselessParent">
<LinearLayout
android:id="@+id/outLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<ListView
android:id="@+id/msgListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/separator"
android:layout_above="@+id/type_message_area"
android:divider="@null"
android:dividerHeight="0dp"
android:paddingBottom="10dp"/>
<include
layout="@layout/type_message_area"
android:layout_width="match_parent"
android:layout_below="@id/msgListView"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="bottom" />
</LinearLayout>
</RelativeLayout>
以下是type_message_area.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal">
<EditText
android:id="@+id/sendConversationEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColorHint="@color/icon_gray"
android:textColor="@android:color/primary_text_light"
android:hint="@string/conversation_edit_text_hint" />
<Button
android:id="@+id/sendConversationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/btn_solid_white"
android:backgroundTint="@color/primary"
android:text="@string/send_conversation"/>
</LinearLayout>
这是我listview
ConversationActivity.java
的滚动条
ListView msgListView = (ListView) findViewById(R.id.msgListView);
//Set autoscroll of listview when a new message arrives
msgListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
msgListView.setStackFromBottom(true);
答案 0 :(得分:0)
您必须在活动级别指定键盘行为。请参阅:https://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
您基本上有2个选项可以解决您的问题。将b.origin()
活动属性设置为:
windowSoftInputMode
:调整活动大小以适应剩余的屏幕空间。这可能会导致adjustResize
调整大小并且ListView
剪辑超出范围的问题。EditText
:不调整活动的大小。它将整个活动平移,并将输入元素的底部与软键盘的顶部对齐。 AFAIK应该给出所需的结果。 (用户总能看到他/她当前正在键入的内容)