【问题标题】:ConstraintLayout View.GONE usageConstraintLayout View.GONE 用法
【发布时间】:2017-12-19 09:10:53
【问题描述】:

我最近开始使用 ConstraintLayout。正如我所发现的,大多数功能都非常简单明了,并且在文档中通过示例、文本和视频教程等进行了很好的解释。

我想到的是如何尽可能优雅地解决这个“难题”?

如您所见,在布局的右侧部分,我有多个左对齐的视图。在最后一行,有 3 个视图水平对齐(它们之间也对齐 TOP)。 问题是:如果我将该行的第一个视图的可见性设置为 GONE,则其他两个(在同一行中)按预期向左移动,但下面的一个(垂直对齐的最后一行)越过之前的行(因为它的constraintTop 属性设置为视图的底部设置为 GONE)。

我能想到的唯一解决方案是使用 ViewGroup 对这 3 个视图进行分组,并将约束添加到最后一行视图。

我是否遗漏了 ConstraintLayout 上的一些可以帮助我的情况的属性?如果其中一个设置在 GONE View 上,可能是某种后备(或多个)约束?

对不起,如果我的解释看起来很深奥,也许这些图片会帮助你更多:)

LE:添加布局:https://gist.github.com/DoruAdryan/7e7920a783f07b865489b1af0d933570

【问题讨论】:

  • 似乎您的视图取决于View.GONE 视图,这不是一个好主意。
  • 是否有另一种方法可以像这样使用不同的视图解决方案来对齐它们?顺序必须保持不变,那些水平对齐的 View 可能每一个都消失了。
  • 我会把每一行做成一个容器,让孩子们决定如果有东西消失了如何对齐。
  • 请使用 xml 代码更新您的问题。您不需要嵌套视图组。
  • @RamiJemli 已更新。

标签: android android-constraintlayout


【解决方案1】:

您可以使用 ConstraintLayout 的新 Barriers 功能。

    <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.support.v7.widget.AppCompatImageView
            android:id="@+id/iv_item_small_product_image"
            android:layout_width="113dp"
            android:layout_height="113dp"
            android:layout_marginLeft="7dp"
            android:layout_marginStart="7dp"
            android:background="@android:drawable/btn_radio"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/iv_row_1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:background="@android:drawable/bottom_bar"
app:layout_constraintLeft_toRightOf="@+id/iv_item_small_product_image"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ro.emag.android.views.FontTextView
            android:id="@+id/tv_row_2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="3dp"
            android:ellipsize="end"
            android:maxLines="2"
            app:layout_constraintLeft_toRightOf="@+id/iv_item_small_product_image"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/iv_row_1"
            app:layout_goneMarginTop="0dp"
            tools:text="Some text long enough to be split on multiple lines on some devices." />

        <android.support.v7.widget.AppCompatRatingBar
            android:id="@+id/rb_row_3_1"
            style="@style/Widget.AppCompat.RatingBar.Small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="9dp"
            android:isIndicator="true"
            android:numStars="5"
            android:stepSize="0.1"
            app:layout_constraintLeft_toRightOf="@+id/iv_item_small_product_image"
            app:layout_constraintTop_toBottomOf="@id/tv_row_2" />

        <TextView
            android:id="@+id/tv_row_3_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginStart="6dp"
            android:layout_marginTop="9dp"
            app:layout_constraintLeft_toRightOf="@+id/rb_row_3_1"
            app:layout_constraintTop_toBottomOf="@id/tv_row_2"
            tools:text="106" />

        <TextView
            android:id="@+id/tv_row_3_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginStart="6dp"
            android:layout_marginTop="9dp"
            app:layout_constraintLeft_toRightOf="@+id/tv_row_3_2"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tv_row_2"
            app:layout_goneMarginLeft="0dp"
            app:layout_goneMarginStart="0dp"
            tools:text="Options available" />

        <android.support.constraint.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="bottom"
            app:constraint_referenced_ids="rb_row_3_1, tv_row_3_2, tv_row_3_3" />

        <TextView
            android:id="@+id/tv_row_4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="3dp"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_constraintLeft_toRightOf="@+id/iv_item_small_product_image"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/barrier"
            tools:text="Some text on last row" />

    </android.support.constraint.ConstraintLayout>

现在,最后一行取决于障碍,而不是第三行的视图之一。由于障碍取决于第二行的三个视图,因此您不会遇到边距问题。
另外,我注意到您不需要该指南。正确的segment可以直接依赖于imageview。

如果您不熟悉障碍,这里有一个 link 可以帮助您。

由于此功能仅在 ConstraintLayout 的 1.1.0 beta1 版本中可用,因此不要忘记将此行添加到您的 build.gradle 文件中。

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

确保包含maven { url "https://maven.google.com" }

【讨论】:

  • 正要添加相同的答案,你不是比我快吗。当我进行更多研究时,似乎这可以解决它。看起来 ConstraintLayout 变得越来越有用,几乎取代了所有其他不可滚动的 ViewGroup。感谢您对我的问题感兴趣!
  • ConstraintLayout 太棒了。您应该检查上一个 beta 版本中的新内容,例如组和占位符。很高兴它有帮助。
猜你喜欢
  • 2020-08-23
  • 1970-01-01
  • 2015-09-10
  • 2011-04-01
  • 1970-01-01
  • 2016-03-31
  • 2019-06-10
  • 1970-01-01
  • 2017-01-18
相关资源
最近更新 更多