【问题标题】:How to show other views after list view height set to fill_parent列表视图高度设置为fill_parent后如何显示其他视图
【发布时间】:2013-11-11 09:17:59
【问题描述】:

为了防止多个 GetView 调用,我将 ListView 的高度设置为 fill_parent。

如何显示除 ListView 本身之外的其他视图?

以下布局在 ViewPager 中膨胀:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp" >

    <ListView android:id="@+id/lv_viol_infraction"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <include layout="@layout/activity_prontuario_home" />
</LinearLayout>

包含的布局在哪里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <ScrollView
        android:id="@+id/prontuariohome_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >
        .
        .
        .
        .
    </ScrollView>

</LinearLayout>

编辑:This solution 不适用于高于按钮的视图

【问题讨论】:

  • 如果你使用 fill_parent 作为高度,你不能这样做。
  • 如何阻止 getview 被调用?它将被调用的次数作为列表项的大小。
  • 不清楚你想达到什么目的

标签: android android-layout listview include


【解决方案1】:

我不明白你想要达到什么目的,但如果你出于某种原因需要将高度设置为 fill_parent 并且不能是任何其他值,你可以将 ListView 与 @987654324 放在一起@ 与 height="0dp"weight="1"。所以会是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp" >

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical" >
        <ListView android:id="@+id/lv_viol_infraction"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <include layout="@layout/activity_prontuario_home" />
</LinearLayout>

【讨论】:

  • 几乎是我找到的解决方案!是的,当然您需要设置 fill_parent,否则您将调用 GetView 方法的次数超过需要的次数,如此处stackoverflow.com/a/2639159/1768336 所述。
【解决方案2】:

尝试使用以下代码

android:layout_height="0dp"
android:layout_weight="1" 

ListView标签中

【讨论】:

  • 使用此解决方案,GetView 将被调用超过需要的次数。已经试过了。
【解决方案3】:

使用 android:layout_height="0dip" 视图将占据剩余的空闲位置

【讨论】:

【解决方案4】:

你说这个解决方案行不通:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/lv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white" android:layout_above="@+id/bt"/>
<Button android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/bt" android:layout_alignParentBottom="true"/>

只需将 android:layout_above="@+id/bt" 添加到列表视图即可。

【讨论】:

  • 是的,因为如果您有一个高度较高的 View,它将与 ListView 重叠。这只是小视图的解决方法。已经尝试添加 layout_above,同样的行为。
  • 你也可以添加'android:minHeight="100dp"'。
  • 这么糟糕的解决方案,我会有一个固定的高度,向列表视图添加更多项目会遇到图形布局问题。
  • 然后添加 'android:maxHeight="100dp"' 在下面查看。
  • 在我的拙见中,您必须使布局尽可能通用。在这种情况下使用 min/maxHeight,你会失去这个“通用规则”
【解决方案5】:

抱歉,不清楚。

这里是(可能)主线程的link,在 Sof 中问及我正在谈论的问题。

经过一些尝试,我很高兴为您解决我的问题。

在 ListView 上方提供另一个布局可以让 LV 削减 ListView 本身未填充的空间

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp" >

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ListView android:id="@+id/lv_viol_infraction"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <include layout="@layout/activity_prontuario_home" />
</LinearLayout>

【讨论】:

  • 为什么不把 wrap_content 放到 ListView 的高度,而不是创建 LL 并将它的高度指定为 Wrap_content 呢?
  • 如下所述,您需要设置 fill_parent,否则您将调用 GetView 方法的次数超过需要的次数,如 stackoverflow.com/a/2639159/1768336 所述。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-22
  • 2016-08-09
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多