简体   繁体   English

约束在较新版本的 android studio 中无法正常工作

[英]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.

相关问题 使用较新版本的Android Studio构建应用后,一年后广告停止工作 - Ads stopped working after a year when app is build from newer version of Android Studio Android Studio无法正常工作 - Android Studio is not working properly 外键约束在sqlite android studio中不起作用 - Foreign key constraints not working in sqlite android studio 主要版本 53 比 52 更新,这是此编译器支持的最高主要版本。 安卓工作室 - major version 53 is newer than 52, the highest major version supported by this compiler. Android studio 已加载“旧” Android Studio项目-Studio更新后,它将不再打开/编译,要求更新版本吗? - Loaded “old” Android Studio project - after Studio updates it will no longer open/compile asking for even newer version? 如何编辑 android studio Java 代码以在较新版本的 Gradle 上工作? - How do I edit an android studio Java code to work on a newer version of Gradle? 相机意图在 android 版本 11 中无法正常工作 - Camera intent is not working properly in android version 11 Android Studio 网格布局无法正常工作 - Android Studio Grid Layout Not Working Properly Android Studio对我来说停止正常工作 - Android Studio stopped working properly for me SQLlite 数据库在 android studio 4 中无法正常工作 - SQLlite database not working properly in android studio 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM