【发布时间】: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