【问题标题】:Android: Can't make ListView full sizeAndroid:无法使 ListView 全尺寸
【发布时间】:2016-03-03 17:27:53
【问题描述】:

我正在尝试将 ListView 设为全屏大小,但每次在我的滚动视图中,我只能从 ListView 中看到一项。 有我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <!-- progress -->
    <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_marginBottom="8dp" android:visibility="gone" />

    <ScrollView android:id="@+id/main_form" android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ListView
                android:id="@+id/countriesList"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </ListView>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

看起来是这样的:

我想让它看起来如何:

如果没有 ListView 的硬编码高度,我无法实现这一点。

【问题讨论】:

  • 2 件事; 1. 您的listview 应始终为wrap_content,您的父线性布局应为match_parent,以及 2. 您不应将列表视图包装在滚动视图中。如果你想要嵌套滚动,你应该使用 recyclerview
  • @tyczj 谢谢你的建议。我被删除了滚动视图:)

标签: android listview


【解决方案1】:

把 ListView 放在 ScrollView 里面是个很糟糕的主意。尽量不要这样做

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <ListView
            android:id="@+id/countriesList"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </ListView>
    </LinearLayout>

【讨论】:

    【解决方案2】:

    因为在您的顶部LinearLayout 中使用padding。所以,你可以删除所有padding

    所以,应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    tools:context=".MainActivity">
    
        <!-- progress -->
        <ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_marginBottom="8dp" android:visibility="gone" />
    
        <ScrollView android:id="@+id/main_form" android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ListView
                    android:id="@+id/countriesList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                </ListView>
            </LinearLayout>
        </ScrollView>
    

    【讨论】:

      猜你喜欢
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多