[英]Android: Scroll down ListView, when keyboard opens
我有以下布局:
<?xml version="1.0" encoding="utf-8"?>
<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="de.blabla.bla.MessageActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mytheme_dark">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/listView2"
android:layout_marginBottom="50dp"
android:dividerHeight="0dp"
android:divider="@null" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-50dp">
<EditText
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/messageInput"
android:background="#9f7348"
android:layout_marginRight="65dp"
android:paddingLeft="5dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Send"
android:id="@+id/sendButton"
android:background="@color/mytheme_color"
android:layout_marginLeft="-65dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
它在下面显示一个Listview和一个文本输入字段。 如果我在文本输入字段上按下,键盘将打开并覆盖一些listview元素。 当键盘打开时,我想向下滚动列表视图。 我怎样才能做到这一点?
编辑:做的时候,答案怎么说,除了键盘的一半与输入字段重叠外,其他方法都有效。 看图片:
在承载ListView的Activity中使用它。 您也可以将其添加到清单中
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
将当前XML更改为此XML:
<?xml version="1.0" encoding="utf-8"?>
<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="de.freshD.crapapp.MessageActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView2"
android:dividerHeight="0dp"
android:divider="@null"
android:layout_above="@+id/messageInput"
android:background="#FFF"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/messageInput"
android:background="#9f7348"
android:padding="12dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/sendButton"
android:layout_toStartOf="@+id/sendButton" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Send"
android:padding="10dp"
android:id="@+id/sendButton"
android:background="#FFF"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/listView2" />
</RelativeLayout>
您还可以在活动清单中放入adjustPan
或adjustResize
:
<application ... >
<activity
android:windowSoftInputMode="adjustPan" ... >
...
</activity>
...
</application>
这是有关此的更多信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.