【问题标题】:Android ListView layout not expaindAndroid ListView 布局不展开
【发布时间】:2018-03-22 16:02:04
【问题描述】:

我有一个带有自定义适配器的简单 ListView。我已经使用 ConstraintLayout 对项目布局进行了建模。一切工作正常,除非某个 TextView 包含长文本,使其位于第二行并与下一个文本重叠。如何使项目仅在需要时垂直扩展?

我的列表视图:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="org.altervista.realms.biomap.fragments.EditFragment">

    <ListView
        android:id="@+id/recordsListView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:divider="@drawable/list_divider"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

我的适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/record_row"
    android:longClickable="true"
    >


    <Button
        android:id="@+id/editRecordDeleteButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:background="@drawable/button"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:minHeight="40dp"
        android:minWidth="40dp"
        android:text="@string/icon_delete"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
    />

    <TextView
        android:id="@+id/editRecordDate"
        android:text="21/06/2015"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="4dp"
    />

    <TextView
        android:id="@+id/editRecordSpecies"
        android:text="Russola Magnificens Russola Magnificens"
        android:textColor="#000000"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/editRecordDate"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/editRecordAbundance"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
    />

    <TextView
        android:id="@+id/editRecordLocHab"
        android:text="Faggeta acidofila a Luzula Croce di Forcella Piccola"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="4dp"
        app:layout_constraintRight_toLeftOf="@+id/editRecordDeleteButton"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        app:layout_constraintTop_toBottomOf="@+id/editRecordSpecies"
    />

    <TextView
        android:id="@+id/editRecordTime"
        android:text="14:44"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/editRecordDeleteButton"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
    />

    <TextView
        android:id="@+id/editRecordAbundance"
        android:text="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/editRecordTime"
        app:layout_constraintRight_toLeftOf="@+id/editRecordDeleteButton"
        android:layout_marginRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
    />

</android.support.constraint.ConstraintLayout>

现在的样子:

我想让第 3 项(仅第 3 项)垂直展开。 谢谢!

编辑:

我注意到只有当文本略长于行时才会出现此问题。使用较长的文本会导致 TextView 按预期展开。

在我看来,它使用父宽度来决定何时扩展布局,而它使用 textview 宽度来决定何时换行。

请注意,以下图片的 XML 是相同的。我只更改了文本字符串。

【问题讨论】:

  • RelativeLayout怎么样?
  • 谢谢,我试试看。

标签: android listview textview android-constraintlayout


【解决方案1】:

从你那里删除app:layout_constraintBottom_toBottomOf="parent" 属性editRecordLocHab textView 解决了我复制代码中的问题。

【讨论】:

  • 谢谢!需要该约束来设置最后一行下方的边​​距。删除它会导致最后一行触及视图的底部,忽略底部边距。我认为 ConstraintLayout 不能很好地处理边距(不是我期望的方式,反正:))
  • 所以它解决了您在问题中面临的问题?
  • 是的。我在最后一个元素下方添加了填充而不是边距,现在一切似乎都在正确的位置。再次感谢您。
  • 很高兴知道!也许您可以将问题标记为已回答?
  • 对不起。我给了+1而不是检查它。完成:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 2014-06-26
  • 2018-02-02
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多