我正在尝试创建一个非常基本的聊天屏幕,其中ListView显示文本,底部是EditText,EditText右侧是“发送”按钮 . 一切都很实用,但当我点击EditText时,虚拟键盘会覆盖它 . 屏幕平移一点但不足以在键盘上方可见 . 我的清单中有“adjustPan”标签,并且还尝试了“adjustResize”标签无效 . 我猜这与我的布局设置方式有关,但老实说我没有任何线索 . 请帮忙!
当前布局......
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/android:list"
android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1"
android:stackFromBottom="true">
</ListView>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/sendMessageBox"
android:focusable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical"
android:maxLines="4"
android:text=""
android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine"
android:maxLength="1000"
android:hint="Type your message..."
android:imeOptions="actionSend"/>
<Button android:id="@+id/sendMessageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Send"/>
</LinearLayout>
7 回答
经过大量搜索后,显然这就是我所说的错误 . 如果您使用全屏标记(从活动中删除状态栏),则不能使用“adjustResize”而不将活动包装在ScrollView中 . 不幸的是,我正在使用ListView,这将产生另一个问题 . 我厌倦了它,可能只是放弃了那个活动的全屏 .
在清单文件中,您需要设置适当的
android:windowSoftInputMode
属性 . 此属性自API 3起有效 .选项包括:http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
stateUnspecified 未指定软键盘的状态(无论是隐藏还是可见) . 系统将选择适当的状态或依赖主题中的设置 . 这是软键盘行为的默认设置 .
stateUnchanged 当活动出现时,软键盘保持在最后的状态,无论是可见的还是隐藏的 .
"stateHidden" 当用户选择活动时隐藏软键盘 - 也就是说,当用户肯定地向前导航到活动时,而不是因为离开另一个活动而退回到活动中 .
stateAlwaysHidden 当活动的主窗口具有输入焦点时,软键盘始终处于隐藏状态 .
stateVisible 当's normally appropriate (when the user is navigating forward to the activity'的主窗口时,软键盘可见 .
stateAlwaysVisible 当用户选择活动时,即当用户肯定地向前导航到活动时,软键盘可见,而不是因为离开另一个活动而退回到活动中 .
adjustUnspecified 未指定活动's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the current focus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window' s内容在较小区域内是否可见 . 这是主窗口行为的默认设置 .
adjustResize 活动的主窗口始终调整大小,以便为屏幕上的软键盘腾出空间 .
adjustPan 活动的主窗口未调整大小以便为软键盘腾出空间 . 相反,窗口的内容会自动平移,以便键盘不会遮挡当前焦点,用户可以随时看到他们正在键入的内容 . 这通常比调整大小更不合适,因为用户可能需要关闭软键盘以获得窗口的模糊部分并与其交互 .
如果为清单中的活动设置
android:windowSoftInputMode="adjustResize"
,则ScrollView(或其他可折叠的ViewGroups)将缩小以适应软键盘 . 但是,如果在活动的主题中设置android:windowFullscreen="true"
,则ScrollView不会缩小,因为它被强制填满整个屏幕 . 但是,在主题中设置android:fitsSystemWindows="false"
也会导致adjustResize不起作用我在AndroidManifest中有这个 . 它导致
adjustPan
停止正常工作 . 我删除了下面的块,一切正常 .这是我找到的一种解决方法 . 打开有问题的editText并按RETURN键 . 请注意,它会将editText移近您正在拍摄的位置 .
因此,虽然hacky,你可以在edittext的顶部取一个新行 .
这似乎也可以在底部使用换行符,但是在软键盘动画到位之后,你必须使用延迟来不添加换行符 .
注意我在某些手机上只有这个问题(DroidX) .
尝试在清单中添加
android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden"
.您可以尝试以下设置: