【问题标题】:ListView not getting space to show content on smaller screensListView 没有空间在较小的屏幕上显示内容
【发布时间】:2013-07-31 17:00:59
【问题描述】:

所以我正在开发一个屏幕,上面有一些图像和按钮,下面是一个列表视图,显示了一些活动的列表。

设计是这样的:-

现在在较小的屏幕上,ListView 的高度变得非常小,因为上面的图标和图像占用了屏幕空间。

那么我怎样才能增加 Linearlayout 或 ListView 的高度,以便用户可以滚动查看 ListView 的其余部分。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
         .... Other Layouts .....

     <ListView
        android:id="@+id/listArea"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="@dimen/list_padding"
        android:paddingRight="@dimen/list_padding" />
</LinearLayout>

编辑:尝试使用顶视图作为列表的标题,但由于我也想要一个 EmptyView,这会产生问题,因为它会替换整个标题 + 列表视图

【问题讨论】:

    标签: android android-listview scroll


    【解决方案1】:

    根据我所读到的关于该问题的信息,您应该将顶部的视图指定为header of the list,这样就可以正常滚动了。

    Afaik 这仅在列表非空时有效,因为空视图会替换整个列表,包括标题。

    【讨论】:

    • 正如我刚刚编辑的那样(忘记链接,抱歉):调用列表中的addHeaderView,使用膨胀的布局。所以你把 Layout 元素放在一个 LinearLayout 或类似的东西中,作为一个单独的 XML 文件,然后从 ListActivity:getListView().addHeaderView(getLayoutInflater().inflate(R.layout.foo, null));
    • 我认为这不会解决问题,因为它会使其他视图固定。现在情况就是这样
    • 我要解决的问题是列表视图的高度在较小的屏幕中非常小。我可以以某种方式增加列表视图的高度,以便用户可以向下滚动以查看列表视图的其余部分吗?
    • 你试过代码了吗?因为我刚刚将这一行放入一个(不相关的)android项目中,并且标题开始随着整个列表滚动(假设列表不为空,如上所述)。
    • 问题是 ListView 正在滚动,并且您不能在一维上同时拥有可滚动的视图。也许(但我不确定)您可以将整个可滚动代码包装到 ScrollView 中,并为列表提供固定高度或 wrap_content。但我没有得到你不喜欢的标题。
    【解决方案2】:

    您可以使用weightSumlayout_weight 属性来控制子视图将占用多少父级可用空间。要使用这些,父布局,例如,您的 LinearLayout,获取 android:weightSum 属性。每个子布局都获得android:layout_weight 属性,其中所有子布局的权重之和就是父布局的 weightSum。此外,每个孩子的layout_heightlayout_width 都应设置为0dp,具体取决于体重。

    这是一个基于您的图表的示例。假设您希望两个顶视图各占屏幕的 1/4,ListView 占屏幕下半部分。将android:weightSum="4" 添加到您的LinearLayout,将android:layout_weight="1" 添加到您用...Other Layouts... 表示的两个子布局,并将android:layout_weight="2" 添加到ListView。代码可能如下所示:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:weightSum="4">
    
        <ImageView
            android:layout_weight="1"
            android:layout_height="0dp"
            ...some other attributes.../>
    
        <LinearLayout
            android:layout_weight="1"
            android:layout_height="0dp"
            android:orientation="horizontal"
            ...some other attributes...>
            ...some children of the LinearLayout...
        </LinearLayout>
    
        <ListView
            android:id="@+id/listArea"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:paddingLeft="@dimen/list_padding"
            android:paddingRight="@dimen/list_padding" 
            android:layout_weight="2"/>
    
    </LinearLayout>
    

    【讨论】:

    • 谢谢@MattDavis 我不想限制上面项目的高度。我只是希望它们在我滚动浏览其余项目时上升。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    相关资源
    最近更新 更多