You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Widget中RemoteViews的ListView最后一项无法完全显示

Android Widget列表最后一项无法完全滚动显示问题

我在Android Widget中通过RemoteViews展示列表,滚动功能正常,但最后一项无法完全滚动出来。不过将Widget完全展开后,最后一项可正常显示。我的代码仅在AppWidgetProvider中设置适配器:

this.setRemoteAdapter(R.id.widgets_list, intent)

Widget布局文件widget_timetable.xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/widget_body_layout"
    style="@style/Widget.AppWidget.AppWidget.Container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/widget_background"
    android:orientation="vertical"
    android:padding="8dp"
    tools:discard="@layout/unused2"
    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*">
    <include layout="@layout/widget_timetable_title_region" />
    <include layout="@layout/widget_timetable_list_meetings" />
    <ListView
        android:id="@+id/widgets_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="2.0sp"
        tools:discard="@layout/unused2"
        tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
        tools:listitem="@layout/widget_item" />
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Loading..."
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
</LinearLayout>

解决方案

这个问题源于Widget未展开时,ListView的高度计算偏差,导致最后一项被截断。可以尝试以下几种修复方式:

  • 给ListView添加底部内边距
    在ListView布局中增加android:paddingBottom="16dp"(数值可根据实际Item高度调整),为最后一项预留足够的显示空间:

    <ListView
        android:id="@+id/widgets_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="2.0sp"
        android:paddingBottom="16dp"
        tools:discard="@layout/unused2"
        tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
        tools:listitem="@layout/widget_item" />
    
  • 改用权重分配剩余空间
    因为父布局是垂直方向的LinearLayout,将ListView的高度设为0dp,并添加android:layout_weight="1",让它自动占据顶部两个include布局之外的剩余空间,避免高度计算错误:

    <ListView
        android:id="@+id/widgets_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:divider="@android:color/transparent"
        android:dividerHeight="2.0sp"
        tools:discard="@layout/unused2"
        tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"
        tools:listitem="@layout/widget_item" />
    
  • 检查顶部include布局的高度设置
    确认widget_timetable_title_region和widget_timetable_list_meetings这两个布局的高度为wrap_content或固定值,若它们使用match_parent会挤压ListView的可用空间,引发滚动异常。

内容的提问来源于stack exchange,提问作者Ilhom Soliev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 07:16:09