【问题标题】:Android ListView in Tab shows only one line标签中的Android ListView 只显示一行
【发布时间】:2017-08-16 23:42:17
【问题描述】:


我对选项卡中 ListView 的大小有疑问。
我认为这很简单,但我无法单独找到解决方案。

Activity 和默认的一样,除了 TextView 用 ListView 改变了。
我使用 ArrayAdapter 和三个项目(“P1”、“P2”、“P3”)的虚拟列表。
所有尺寸都设置为“match_parent”或“wrap_content”

我得到的是:我只看到“P1”而不是整个列表,带有一个允许在值之间滚动的小滚动条。
我想看到的是整个列表。

screenshot of my app

有人能帮我吗?

主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="it.danapps.mycommunity.ChooseTags">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/container_choose_tags"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />



片段

<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"
tools:context="it.danapps.mycommunity.ChooseTags$PlaceholderFragment">


<ListView
    android:id="@+id/chooseTagListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     />



编辑
1. 将高度强制为 300dp 我看到了列表。但是我怎样才能自动适应Android,适应屏幕呢?

<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"
tools:context="it.danapps.mycommunity.ChooseTags$PlaceholderFragment">


<ListView
    android:id="@+id/chooseTagListView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
     />



【问题讨论】:

  • 尝试将 ListView 和 NestedScrollView 的 android:layout_height 更改为 match_parent。
  • 我试过了,但没有任何改变

标签: android listview


【解决方案1】:

如果您将 TextViews 设置为“match_parent”,那将是您的问题。确保您的 TextViews 使用设置的高度或使用“wrap_content”。否则,您的 TextView 将尝试填充其放置的滚动视图的高度,这就是您只看到第一个的原因。

【讨论】:

  • 是的,将高度强制为 300dp 我看到了列表。但是我怎样才能自动适应Android,适应屏幕呢?
  • 不确定您的意思?如果您需要滚动视图中的每个单元格作为屏幕的高度,您可以通过编程将子参数的布局参数设置为屏幕高度。如果您需要 TextView 的高度与它包含的文本一样高,您可以使用“wrap_content”
  • 我想在屏幕上查看整个列表:查看所有元素直到屏幕可用(其余元素将使用滚动条显示)。试着更好地解释我:在屏幕上我有空间看到 3 个元素,但如果我使用“Match_parent”或“fill_content”,我只能看到一个;将高度强制为 dp(例如 300),我正确地看到了 3 个元素。通过同时放置此屏幕截图来更新帖子
  • 好的,我看到您使用 300dp 作为 ListView 高度。那是不对的。 ListView 可以是“match_parent”。但是 TextView 子项,那些被添加到滚动 ListView 的文本,需要将它们的高度设置为“wrap_content”或 dp 中的某个所需高度。所以重申一下,ListViews 的高度应该是“match_parent”,这将为您提供屏幕的可用高度。并且子视图(包含 P1 等的 TextViews)需要设置高度或“wrap_content”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多