android - 尝试使用 ConstraintLayout 复制 GridLayout 列对齐

标签 android android-layout android-gridlayout android-constraintlayout

我是 ConstraintLayout 的新手,我正在尝试使用 ConstraintLayout 复制 GridLayout 提供的相同网格行为。

具体来说,我想设计一个两列的网格。第一列宽度应尽可能窄,而第二​​列应占用所有剩余的水平空间。当然,第二列应该位于第一列的右侧,或者更确切地说,位于第一列的最宽 View 。

我不知道如何使用 ConstraintLayout 复制最后一个要求。我不想在两列之间使用网格线,因为第一列不应具有固定宽度或百分比宽度,而应与其 View 的最宽宽度一样宽。

https://gist.github.com/venator85/499dd82f47b3efbbed7a1e9e1ca1412d我准备了一个布局示例和相应的预览,显示了实现我想要的内容的 GridLayout。该布局中的前两个 ConstraintLayout 尝试显示 C1 和 D1 与 B1 对齐,C2 和 D2 与 B2 对齐。当 B2 比 A2 窄时,A1 和 C1 会重叠。

有什么帮助吗?

谢谢

最佳答案

Google 在最新版本的 ConstraintLayout 中引入了“障碍”的概念,这有助于在 XML 中 100% 解决这个问题。查看ConstraintLayout 1.1.0 beta 1 release notes .虽然,该说明不包含有关新功能的大量信息,但有一个 talk at I/O 2017触及了新的东西。

新的解决方案是用障碍复制GridLayout的网格。在左侧 TextView 的右侧放置了一个垂直屏障,在前三行下方放置了一个屏障。障碍根据每个 TextView 中存在的文本量而变化,但始终保持在 app:constraint_referenced_ids 中指定的位置。从本质上讲,障碍就像 float 指南。此解决方案不依赖任何编码来支持视频中的内容。

这是一个video of the new layout这显示了随着另一个 TextView 的内容更改而维护的每个 TextView 的所需定位。该视频是在 Android Studio 2.3.2 的设计工具中制作的。

以及使用障碍的新布局的 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:id="@+id/constrained"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.constraint.Barrier
        android:id="@+id/barrierVertical"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:barrierDirection="right"
        app:constraint_referenced_ids="L1, L2, L3, L4" />

    <TextView
        android:id="@+id/L1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="L1 *"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/R1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1 R1*"
        app:layout_constraintLeft_toRightOf="@+id/barrierVertical"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="L1, R1" />

    <TextView
        android:id="@+id/L2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="L2 L2*"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier1"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/R2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2 R2*"
        app:layout_constraintLeft_toRightOf="@+id/barrierVertical"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier1"
        tools:ignore="HardcodedText" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="L2, R2" />

    <TextView
        android:id="@+id/L3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="L3 L3 L3*"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier2"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/R3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3 R3*"
        app:layout_constraintLeft_toRightOf="@id/barrierVertical"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier2"
        tools:ignore="HardcodedText" />

    <android.support.constraint.Barrier
        android:id="@+id/barrier3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="L3, R3" />

    <TextView
        android:id="@+id/L4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="L4 L4 L4 L4 L4 L4*"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barrier3"
        tools:ignore="HardcodedText,RtlHardcoded" />

    <TextView
        android:id="@+id/R4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:text="R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4 R4*"
        app:layout_constraintLeft_toRightOf="@+id/barrierVertical"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barrier3"
        tools:ignore="HardcodedText" />

</android.support.constraint.ConstraintLayout>

关于android - 尝试使用 ConstraintLayout 复制 GridLayout 列对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42846261/

相关文章:

android - 选择器与 android :drawable ="@color/transparent" attribute

android - 如何将 View 放在确定的像素/位置

android - 如何在 BottomNavigationView 中添加自定义背景

java - Android Studio 网格布局无法正常工作

java - 如何使用Java在Android GridLayout中设置间距?

android - 有什么办法可以嵌入可滚动的标签吗?

安卓 : Exif of a Byte[] or Bitmap object

android - Toast 不显示 - Android

android - Phonegap - 为 Android 应用添加启动画面

java - 选中时如何更改卡片 View 的颜色?单击