在父 ConstraintLayout 中水平居中固定大小的 ImageView [英] Center fixed-size ImageView horizontally in parent ConstraintLayout
问题描述
我有一个相当简单的布局:一个 ConstraintLayout
填满整个屏幕,一个大的 CardView
在顶部有一个 ImageView
> 一半在 CardView
上,一半在上面(实际上不是一半,但你懂的).
I have a rather simple layout: A ConstraintLayout
filling the entire screen, with one big CardView
that, at the top, has an ImageView
that is half on the CardView
and half above it (actually not half, but you get the point).
但是,有两个问题:ImageView
粘在父项的左侧,我不知道如何使其水平居中 - 我在 Google 上只能找到多个元素的链,但我只想要一个居中的.
However, there are two issues: the ImageView
sticks to the left of the parent and I do not know how to make it horizontally centered - all I can find on Google are chains for multiple elements, but I just want one centered.
布局编辑器对我来说非常糟糕.我的第一个抱怨是我似乎无法对父级设置约束.然后我尝试通过按下在父级中居中"按钮将 ImageView
水平居中在父级中.这增加了 CardView
的宽度,对 ImageView
完全没有影响.
The layout editor works extremely bad for me. My first complaint is that I seem to be unable to create constraints to the parent. Then I tried centering the ImageView
horizontally in parent by pressing the button that says "Center in parent". This increases the CardView
's width, no effect at all on the ImageView
.
当我在做的时候,我怎么能在 CardView
顶部对 ImageView
进行 z 排序?
While I am at it, how could I z-order the ImageView
on top of the 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" />
谢谢!
推荐答案
根据 文档:
当您向视图的两侧添加约束(以及视图大小对于相同的维度是固定"或包装内容"),视图默认情况下会在两个锚点之间居中.
When you add a constraint to both sides of a view (and the view size for the same dimension is either "fixed" or "wrap content"), the view becomes centered between the two anchor points by default.
添加以下约束以 ImageView
为中心.
Adding the following constraint centered the ImageView
.
app:layout_constraintEnd_toEndOf="parent"
此外,添加比 CardView
更高的海拔固定了 z-index,因为海拔覆盖了 z-index.
Also, adding a higher elevation than the CardView
fixed the z-index, since the elevation overrides z-index.
这篇关于在父 ConstraintLayout 中水平居中固定大小的 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!