安卓如何判断是否将布局上移以显示软键盘?

6
Android如何确定在显示软键盘时是否将布局向上移动?
注意:我知道activity属性android:windowSoftInputMode="adjustResize|adjustResize|adjustUnspecified"存在,如此描述 http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft ,但在我的情况下,它似乎没有任何效果。这是我的问题:
我有两个活动,布局几乎相同,但第一个活动使用一个包含按钮列表的ListView。第二个活动包含一个带有按钮的scrollview。其余部分相同,按钮数量相同,元素高度相同等。
现在,当我按搜索按钮打开搜索输入栏时,在我的第一个活动中,整个布局都被移动了。而在第二个活动中,布局没有被移动,而是软键盘直接显示在其上方。这实际上是我想要的行为。如何在使用ListView的活动中实现相同的行为?
在我的清单文件中,最初我没有指定任何android:windowSoftInputMode属性,但即使我这样做,也没有任何区别;我尝试了所有三个值(adjustPan、adjustResize、adjustUndefined),但都没有任何区别。
这是我的布局:
1) http://pastebin.com/5zzVxjbK 2) http://pastebin.com/KFtPuHvP 有趣的是:当我在布局1(左侧)中设置我的ListView可见性为View.INVISIBLE时,布局就不会向上移动!

alt text


@Mathias Lin:我认为你需要发布一些代码,仅有listviewscrollview并不能做任何事情。我猜这取决于布局。 - Praveen
这里是代码,'button section' 在两种情况下都被命名为android:id="@+id/category_questions";不确定那是否是相关部分。1) http://pastebin.com/5zzVxjbK, 2) http://pastebin.com/KFtPuHvP - Mathias Conradt
是的,我确定这与布局有关,但是怎么相关呢?Android开发者文档在这方面并没有提供太多信息。 - Mathias Conradt
@Mathias - 你尝试过在ListView上设置android:isScrollContainer="false"吗?也许再加上"adjustPan"会有帮助。我不确定这是否有用,但很好奇。 - Paul Burke
另外,我注意到的一个区别是ScrollView使用android:layout_height="wrap_content",而ListView使用"fill_parent"。不确定这是否会有任何影响。 - Paul Burke
@iPaulPro:我刚试了在ListView/活动中设置android:isScrollContainer="false","adjustPan",android:layout_height="wrap_content",但不幸的是它完全没有任何区别。 - Mathias Conradt
1个回答

0
我可以使用 RelativeLayout 并设置 android:windowSoftInputMode="adjustPan" 来实现这一点:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent" >

    <include android:id="@+id/Header" layout="@layout/header"
        android:layout_width="fill_parent" android:layout_height="wrap_content" />

    <ListView android:id="@+id/android:list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_below="@id/Header"
        android:layout_above="@+id/Footer"  />

    <include android:id="@id/Footer" layout="@layout/footer"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />
</RelativeLayout>

如果您想保留LinearLayout,可能可以通过将页脚重力(或layout_gravity)设置为“bottom”来实现此目的,但我不确定。


我在我的应用程序中有其他活动(即包含一个填充屏幕90%的listView,只有一个小区域用于选项卡标题),其中外部布局是线性布局,在那里我没有遇到这种效果。我只在某些活动中看到它。我相信你上面的布局是有效的,但我想了解其背后的逻辑,以便我知道如何更改现有的LinearLayout和/或导致此行为的确切原因。我确定有解决方法,但我不想对症状进行斗争,而是要解决根源问题。 - Mathias Conradt
你尝试过在底部设置footer的gravity(layout_gravity)为“bottom”,并使用“adjustPan”吗? - Paul Burke
似乎ListViews会被“移入视图”,除非它们被锚定在屏幕底部,并使用adjustPan。这种默认行为可以防止软键盘覆盖列表项。 - Paul Burke
我尝试按照所述设置页脚的重力,但没有任何区别。我不明白为什么ListView的行为与ScrollView不同,两者都是滚动容器,并且我以相同的方式将它们放入我的布局中。 - Mathias Conradt
我不确定,但我知道ListView不仅仅是一个滚动容器。您可能需要检查各自的源代码以确定差异。或者只需使用RelativeLayout :) - Paul Burke

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接