android – ConstraintLayout View.GONE用法

前端之家收集整理的这篇文章主要介绍了android – ConstraintLayout View.GONE用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我最近开始使用ConstraintLayout.正如我发现的那样,大多数功能都非常简单,并且在文档中对样本,文本和视频教程等进行了详细解释.

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


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

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

我错过了ConstraintLayout上的一些属性,这对我的情况有帮助吗?如果在GONE View中设置其中一个,可能会出现某种后退(或多个)约束?

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

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

解决方法

您可以使用ConstraintLayout的新“障碍”功能.
<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>

现在,最后一行取决于屏障而不是第三行的其中一个视图.由于障碍取决于第二行的三个视图,因此您不会出现边缘问题.
另外,我注意到你不需要指南.右段可以直接依赖于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”}

原文链接:https://www.f2er.com/android/314293.html

猜你在找的Android相关文章