我有一个照片上传 Activity ,照片作为主要背景,底部有一个编辑文本和一组按钮。当我点击 edittext 时,我希望软键盘将 edittext 和按钮向上推(这样它们仍然可见)并保持背景图像不变。但是,现在它只将它向上推到足以看到编辑文本的第一行并且我的所有按钮都被隐藏了。
我基本上想以某种方式将 View 附加到软键盘上,而让 Activity 的其余部分保持不变。我已经尝试在 list 中设置 windowSoftInputMode 标志,但没有一个选项产生我想要的效果(我不希望布局调整大小和平移似乎没有影响)。我如何实现这样的目标?
最佳答案
不幸的是,在当前的 SDK 中,当涉及到软键盘出现和消失时 View 的行为方式时,我们有点受制于系统。实现您正在寻找的行为的最佳方法是在窗口上保留默认输入模式,并确保您的 View 中有一个可滚动元素。请记住,当键盘隐藏时(内容小于 ScrollView ),此元素不需要滚动,但当键盘出现时,它会折叠可 ScrollView 的大小并保留其他所有内容。
这是一个例子。假设这是您的 res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/control"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A Button"/>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/control">
</ScrollView>
</RelativeLayout>
尝试将其作为基本 Activity 的根布局,您会看到整个 LinearLayout
使用键盘上下滑动,因为 Android 正在调整 ScrollView
的视口(viewport)大小而不是向上滑动 View ,只需足以显示聚焦的输入元素(您现在看到的)即可。
关于java - 在 Android 中,如何将 View 附加到软键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821168/