【问题标题】:Android: ConstraintsLayout VS No Constraints ViewGroupsAndroid:ConstraintLayout VS 无约束 ViewGroup
【发布时间】:2017-07-13 13:14:35
【问题描述】:

我有两个提供相同结果的布局。第一个是在没有约束的情况下创建的,第二个是使用约束创建的。结果由两个兄弟部分(一个在另一个之上)组成,竞争可用空间。底部部分优先于顶部部分,因此后者必须符合底部部分不需要的空间。此外,底部部分的顶部由具有其内容的顶部给出。 Example image

为了能够使用约束来实现这种行为,我不得不将底部部分嵌套在其他约束布局中。是否有另一种方法可以在不使用嵌套的情况下使用 ConstraintLayout 实现相同的行为? 我发现的另一个问题是,当我要为嵌套 ConstraintLayout 内的视图的约束设置动画时,不执行任何动画。外部视图是的。是否可以执行嵌套约束动画? 这些是使用的 XML 文件:

无约束版本:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/top_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_container">

        <Button
            android:id="@+id/remove_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bottom_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="bottom"
        android:layout_alignParentBottom="true">

        <ImageView
            android:id="@+id/car_image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/ic_directions_car_black_24dp"/>

        <ImageView
            android:id="@+id/loto_image"
            android:layout_width="60dp"
            android:layout_height="80dp"
            android:src="@drawable/ic_spa_black_24dp"/>
    </LinearLayout>

</RelativeLayout>

ConstraintLayout 版本:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_constlayout_container">

    <View
        android:id="@+id/top_container_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom_constlayout_container"/>

    <Button
        android:id="@+id/remove_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/bottom_constlayout_container"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <ImageView
            android:id="@+id/car_image"
            android:layout_width="60dp"
            android:layout_height="60dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            android:src="@drawable/ic_directions_car_black_24dp"/>

        <ImageView
            android:id="@+id/loto_image"
            android:layout_width="60dp"
            android:layout_height="80dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toRightOf="@id/car_image"
            android:src="@drawable/ic_spa_black_24dp"/>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

问候!

【问题讨论】:

    标签: android android-layout android-animation android-constraintlayout android-viewgroup


    【解决方案1】:

    如果您更新到 ConstraintLayout 的 1.1.x 版本,您可以使用屏障分隔两个部分,如下所示。屏障漂浮在两个ImageViews 上方,上部的底部被限制在屏障的顶部,因此,随着屏障浮动,顶部的底部也一样。

    HereConstraintLayout 1.0.1 beta 1 的发行说明。此堆栈溢出answer 引用了一些有关ConstraintLayout 新功能的文档。

    如果您知道其中一个图像视图总是比另一个更高,您可以放弃障碍,只需将上视图的底部与较高的图像视图的顶部联系起来。

    如果您进行这些更改,您可以看到您在动画中所处的位置。

    <View
        android:id="@+id/top_container_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FF00FF00"
        app:layout_constraintBottom_toTopOf="@+id/barrier"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <Button
        android:id="@+id/remove_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:barrierDirection="top"
        app:constraint_referenced_ids="car_image,loto_image" />
    
    <ImageView
        android:id="@+id/car_image"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/ic_directions_car_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />
    
    <ImageView
        android:id="@+id/loto_image"
        android:layout_width="60dp"
        android:layout_height="189dp"
        android:src="@drawable/ic_spa_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toRightOf="@id/car_image"
        tools:layout_editor_absoluteY="322dp" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 2022-09-28
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 2016-12-27
      • 2012-12-28
      相关资源
      最近更新 更多