2

我所拥有的是一个以照片为主要背景的照片上传活动,底部有一个编辑文本和一组按钮。当我单击编辑文本时,我希望软键盘向上推动编辑文本和按钮(因此它们仍然可见)并保持背景图像相同。但是,现在它只能将其推高到足以看到编辑文本的第一行并且我的所有按钮都被隐藏了。

我基本上想以某种方式将视图附加到软键盘上,而让其余的活动不理会。我尝试在清单中设置 windowSoftInputMode 标志,但没有任何选项产生我想要的效果(我不希望调整布局大小并且平移似乎没有影响)。我如何实现这样的目标?

4

1 回答 1

1

不幸的是,在当前的 SDK 中,当涉及到软键盘出现和消失时视图的行为方式时,我们有点受系统的支配。实现您正在寻找的行为的最佳方法是保留窗口上的默认输入模式,并确保您的视图中有一个可滚动元素。请记住,当键盘隐藏时(内容小于滚动视图),此元素不需要滚动,但是当键盘显示时,它会折叠可滚动视图的大小并保留其他所有内容。

这是一个例子。假设这是您的 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而不是向上滑动视图以显示聚焦的输入元素(您现在看到了)。

于 2011-01-27T20:59:50.297 回答