【问题标题】:How to use constraint layout in ListView如何在 ListView 中使用约束布局
【发布时间】:2019-07-08 20:17:25
【问题描述】:

我一直在为我的列表视图使用线性布局,如果我的数据只是像 但是当我确实想为 ConstraintLayout 格式化我的数据时,它需要一整页才能滚动,而不是像线性布局那样。

示例代码 android.support.constraint.ConstraintLayout

<TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.019"
        app:layout_constraintStart_toEndOf="@+id/textView7"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textViewNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="312dp"
        android:layout_marginBottom="668dp"
        android:text="Number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toEndOf="@+id/textView11"
        app:layout_constraintTop_toBottomOf="@+id/textViewName"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="July"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />`

【问题讨论】:

  • 是带bean的自定义适配器吗?
  • 发布您的完整 xml 代码。
  • 在 ListView 项目中使用约束布局确实是个坏主意。它会降低您的滚动性能。
  • 在 UI 文件中,采用任何布局并将 ListView 标记放入其中。在以编程方式将数据绑定到 ListView 之后。无需设置任何内容即可滚动。
  • @MayurRaval 感谢您让我知道这件事。

标签: java android layout android-constraintlayout


【解决方案1】:

在使用约束布局时,请确保为视图提供正确的约束。如我所见,您将 bottom_to_bottomOf = "parent" 减半,这使视图获得父视图的完整高度。

只需从您的视图中删除 app:layout_constraintBottom_toBottomOf="parent" 。

您需要为任何视图提供至少三个约束。检查这是否工作正常???

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
    android:id="@+id/textViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Name"
    android:textAppearance="@style/TextAppearance.AppCompat.Large"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.019"
    app:layout_constraintStart_toEndOf="@+id/textView7"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/textViewNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="312dp"
    android:layout_marginBottom="668dp"
    android:text="Number"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/textView11"
    app:layout_constraintTop_toBottomOf="@+id/textViewName"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/textView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="July"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

为了更好地理解约束布局,您可以使用此参考:https://medium.com/@loutry/guide-to-constraintlayout-407cd87bc013

另外,你不需要写

    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"

相反,您可以使用 android:layout_margin="8dp" 覆盖上述四个边距。

【讨论】:

  • 谢谢!这解决了它。我很欣赏 ConstraintLayout 链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2018-07-17
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-04
相关资源
最近更新 更多