繁体   English   中英

在Android中,如何将视图附加到软键盘?

[英]In Android, how do I attach a view to the soft keyboard?

我所进行的是一个照片上传活动,其中照片为主要背景,底部为edittext和一组按钮。 单击编辑文本时,我希望软键盘将编辑文本和按钮向上推(以便它们仍然可见),并保持背景图像不变。 但是,现在它仅将其向上推足以看到edittext的第一行,而我的所有按钮都被隐藏了。

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

不幸的是,在当前的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的视口的大小,而不是向上滑动视图就足以显示聚焦的输入元素(您现在看到的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM