【问题标题】:Resizing ListView when keyboard is present存在键盘时调整 ListView 的大小
【发布时间】:2014-10-21 11:49:58
【问题描述】:

当存在软件键盘时,我在调整列表视图大小时遇到​​了一点问题。基本上我使用的是谷歌地图 API,其中地图覆盖了整个屏幕,带有透明的操作栏,并且我有一个从右侧滑入的片段中存在于地图上的车辆列表。当右侧面板出现时,整个布局如下所示:

现在我使用的是android:windowSoftInputMode="adjustPan",所以当我聚焦编辑文本并且键盘滑出时,它看起来像这样:

这不好,因为我看不到完整的车辆列表。我曾尝试使用android:windowSoftInputMode="adjustResize",但由于我使用的是透明操作栏,因此会发生这种情况: 我可以看到整个 listView 并且它滚动得很好,但是我的 EditText 现在位于 ActionBar 下。基本上我想要实现的是两种情况的结合,所以它看起来像这样: 现在这是我的列表的片段布局

<?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>

我熟悉以编程方式设置布局的高度,因为我已经在使用嵌套了 listfragment 的布局。 ActionBar 必须保持透明,因为这是我老板想要的。

【问题讨论】:

    标签: android android-layout android-fragments android-listview


    【解决方案1】:

    我相信,因为您的操作栏类似于 Google 地图使用的操作栏,所以通常由它占用的空间被认为可供其他布局使用,在您的情况下,片段。您可以尝试为片段容器指定填充或边距,例如:

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

    (操作栏的大小)。

    一起

    android:windowSoftInputMode="adjustResize"
    

    这应该可以。

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

    【讨论】:

    • 我不得不更改我的 MainActivity.xml 并将 alignParentBottom 更改为 alignParentTop,但这有效,所以我将其标记为答案,谢谢
    • 它是否适用于 android:paddingTop="?android:attr/actionBarSize 或者您是否必须更改该值?我之所以问是因为我自己没有机会对此进行测试。跨度>
    • 是的,它工作得很好,不敢相信我自己没有弄清楚这一点
    【解决方案2】:

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

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

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

    【讨论】:

    • 我现在正在使用android:windowSoftInputMode="adjustPan",它看起来像第二张图片,我已经在我的问题中说明了这一点,所以这对我不起作用。
    【解决方案3】:

    API 说明如下 http://developer.android.com/reference/android/widget/ListView.html

    TRANSCRIPT_MODE_ALWAYS_SCROLL = 无论当前可见哪些项目,列表都会自动滚动到底部。

    setTranscriptMode(int mode) = 将列表或网格置于转录模式。

    请测试,看看是否有效!

    setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    

    .

    【讨论】:

      【解决方案4】:

      在AndroidManifest.xml中添加:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-12
        • 2012-09-29
        • 2013-02-14
        • 1970-01-01
        • 2018-02-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多