[英]Constraints not working properly in a newer version of android studio
I'm trying to center those 2 images in the center of the layout, without using any margins, just constraints.我试图将这 2 个图像居中在布局的中心,不使用任何边距,只是约束。 I've copied my instructor code, but in my version of android studio it's not working.
我已经复制了我的讲师代码,但是在我的 android studio 版本中它不起作用。 Even if I download his activity-main.xml file, the bug is still there.
即使我下载了他的activity-main.xml 文件,bug 仍然存在。 You can see in the photos what I mean.
你可以在照片中看到我的意思。 This is my code / This is his
这是我的代码/这是他的
You need to chain both of your images horizontally and constraint one of them to the side of the screen, by doing that both of your images will be centered:您需要水平链接两个图像并将其中一个限制在屏幕的一侧,这样您的两个图像都将居中:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">
<ImageView
android:id="@+id/imageView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@tools:sample/avatars[0]" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/imageView5"
app:layout_constraintTop_toBottomOf="@+id/imageView3"
tools:src="@tools:sample/avatars[0]" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/imageView4"
app:layout_constraintEnd_toStartOf="@+id/imageView4"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView4"
tools:src="@tools:sample/avatars[0]" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
After that your layout will look like this (when I cut the screenshot I did it a bit asymmetry but don't worry your layout will be good):之后,您的布局将如下所示(当我剪切屏幕截图时,我做了一点不对称,但不要担心您的布局会很好):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.