当键盘出现时调整ListView大小

3
我在使用Google地图API时遇到了一个问题,当软键盘出现时无法调整列表视图的大小。基本上我将地图覆盖整个屏幕,透明ActionBar,并在右侧滑动碎片中显示一系列车辆。当右侧面板展开时,整个布局如下所示:enter image description here 现在我正在使用android: windowSoftInputMode="adjustPan",因此当我聚焦于EditText并弹出键盘时,它看起来像这样:enter image description here 这不好,因为我无法看到整个车辆列表。我已经尝试使用android:windowSoftInputMode="adjustResize",但由于我使用了透明ActionBar,因此会出现以下情况:enter image description here
我可以看到整个ListView,并且它可以很好地滚动,但是我的EditText现在位于ActionBar下方。基本上,我想要实现两种情况的组合,使其看起来像这样:enter image description here。现在这是带有我的列表的碎片的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context="hidden">

    <EditText
        android:id="@+id/vehicle_search_edit_text"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:focusable="true"
        android:hint="Search"
        android:imeOptions="actionSearch"
        android:singleLine="true" />

    <ListView
        android:id="@+id/list_vehicles"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/vehicle_search_edit_text"
        android:animateLayoutChanges="true"
        android:fastScrollEnabled="true"
        />
</RelativeLayout>

这是我的MainActivity的布局。
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/layout_root_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RelativeLayout
        android:id="@+id/layout_root_vehicles"
        android:layout_width="350dp"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

    <RelativeLayout
        android:id="@+id/layout_root_settings"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

我熟悉通过编程设置布局高度,因为我已经在嵌套列表片段的布局中这样做了。ActionBar必须保持透明,因为那是我的老板想要的。

4个回答

4

我认为,因为你的ActionBar与例如Google Maps使用的ActionBar相似,所以它通常占用的空间被认为是可供其他布局使用的,对于你的情况,就是Fragment。你可以尝试为你的Fragment容器指定填充或边距,例如:

android:paddingTop="?android:attr/actionBarSize"

(操作栏的大小)。
与此同时,
android:windowSoftInputMode="adjustResize"

这应该可以工作。

如果填充值不满足您的要求,请手动提供更合适的值。


我不得不修改我的MainActivity.xml文件,将alignParentBottom更改为alignParentTop,但这样做有效,因此我将其标记为答案,谢谢。 - Marcel Krivek
它是否与android:paddingTop="?android:attr/actionBarSize"一起工作,还是您必须更改该值?我之所以问这个问题,是因为我自己没有测试过。 - Adrian Sicaru
是的,它完美地工作了,我简直不敢相信我自己没有想到这个。 - Marcel Krivek

0
在您的清单文件中,在活动中添加android:windowSoftInputMode="adjustPan"。 例如,

<activity
            android:name="com.sample.ListViewsActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" >
        </activity>

我认为这可以解决你的问题。试试看吧。


我现在正在使用 android:windowSoftInputMode="adjustPan",但它看起来像第二张图片,我已经在我的问题中说明了这一点,所以这对我没有用。 - Marcel Krivek

0

API显示如下内容 http://developer.android.com/reference/android/widget/ListView.html

TRANSCRIPT_MODE_ALWAYS_SCROLL = 列表将自动滚动到底部,无论当前可见的项目是什么。

setTranscriptMode(int mode) = 将列表或网格设置为记录模式。

请进行测试,看看是否有效!

setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

.


0

在 AndroidManifest.xml 中添加:

<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize"/>
</activity> 

为您的活动添加两个参数:

<ListView
android:stackFromBottom="true"
android:transcriptMode="normal"/>

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