在父ConstraintLayout中水平居中固定大小的ImageView

时间:2017-01-26 21:20:06

标签: android android-layout android-constraintlayout

我有一个相当简单的布局:ConstraintLayout填充整个屏幕,其中一个大的CardView位于顶部,ImageViewCardView的一半ImageView和它上面的一半(实际上不是一半,但你明白了。)

然而,有两个问题:ImageView粘在父母的左边,我不知道如何让它水平居中 - 我在Google上找到的只是多个元素的链条,但我只是希望一个人居中。

布局编辑器对我来说非常糟糕。我的第一个抱怨是我似乎无法为父母创建约束。然后我尝试通过按下“父母中心”按钮将父母CardView水平居中。这会增加ImageView的宽度,对ImageView毫无影响。

虽然我在这,但我如何在CardView之上对<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimaryDark"> <android.support.v7.widget.CardView android:layout_width="0dp" android:layout_height="0dp" android:layout_marginBottom="32dp" android:layout_marginEnd="32dp" android:layout_marginStart="32dp" android:layout_marginTop="160dp" app:cardCornerRadius="12dp" app:cardElevation="32dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent"> </android.support.v7.widget.CardView> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:layout_marginTop="40dp" android:scaleType="centerCrop" android:src="@drawable/re_zero" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> 进行z订购?

这里布局:

.pipe(imagemin({optimizationLevel: 5, progressive: true, interlaced: true}))

谢谢!

1 个答案:

答案 0 :(得分:12)

根据docs

  

向视图的两侧添加约束(以及视图大小   对于相同的维度,可以是“固定”或“包装内容”),视图   默认情况下,它们会在两个锚点之间居中。

添加以下约束集中于ImageView

app:layout_constraintEnd_toEndOf="parent"

此外,添加比CardView更高的高程来修复z-index,因为高程会覆盖z-index。

相关问题