使用分隔线间距定义列表视图项时出现 Constraintlayout 问题

Issue with Constraintlayout when defining the listview items with the divider spacing

在使用 ConstraintLayout(我没有使用 RelativeLayout)时,我有一个关于列表视图分隔线的愚蠢问题。

下面是描述我的问题的图片。 space 上分频器与下分频器不一样:

这是我的 xml 文件。 我需要做什么才能使文本位于中间?

<?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">


    <TextView
        android:id="@+id/appd_permlist"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="60dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        android:textSize="@dimen/a_xlarge"
        app:layout_constraintEnd_toStartOf="@+id/appd_risk"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/appd_risk"
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_marginEnd="60dp"
        android:background="@color/lgrey"
        app:layout_constraintBottom_toBottomOf="@+id/appd_permlist"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/appd_permlist" />
</android.support.constraint.ConstraintLayout>

我要做一些假设:

  • 行的高度应由 appd_risk 视图定义(因为它具有固定高度)
  • appd_permlist 应在如上定义的 space 中垂直居中

在那种情况下,这是一个可行的布局:

<?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">

    <TextView
        android:id="@+id/appd_permlist"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="60dp"
        android:textSize="@dimen/a_xlarge"
        android:text="TextView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/appd_risk"
        app:layout_constraintStart_toStartOf="parent"/>

    <TextView
        android:id="@+id/appd_risk"
        android:layout_width="90dp"
        android:layout_height="30dp"
        android:layout_marginEnd="60dp"
        android:background="@color/lgrey"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

</android.support.constraint.ConstraintLayout>

我已经将 ConstraintLayout 的高度更改为 wrap_content,然后我将 appd_risk top/bottom 约束更改为 parent,这使得行为 30dp高(或您为 appd_risk 选择的任何尺寸)。

然后我从 appd_permlist 中删除了顶部边距,并另外向 parent 添加了底部约束。这将垂直居中 appd_permlist.