gpt4 book ai didi

java - 约束布局问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:39:20 24 4
gpt4 key购买 nike

我在尝试创建新布局时遇到一些问题。我使用的是 ConstraintLayour,它以 ImageView 作为背景,并有 6 个按钮(A、B、C、D、E、F),它们应该在 ImageView 上有固定的位置。 Android Studio 中的预览如下所示: /image/urDU7.jpg

当我启动应用程序时,我得到以下信息: /image/Cu9wJ.jpg

这是代码:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Mapa">

<ImageView
android:id="@+id/almacen"
android:layout_width="0dp"
android:layout_height="0dp"

android:adjustViewBounds="true"
android:cropToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/almacen_fondo" />

<Button
android:id="@+id/a_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginTop="140dp"
android:backgroundTint="#FFC107"
android:text="A"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/b_button"
app:layout_constraintStart_toStartOf="@+id/b_button"
app:layout_constraintTop_toBottomOf="@+id/b_button" />

<Button
android:id="@+id/b_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginTop="140dp"
android:backgroundTint="#FFC107"
android:text="B"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/c_button"
app:layout_constraintStart_toStartOf="@+id/c_button"
app:layout_constraintTop_toBottomOf="@+id/c_button" />

<Button
android:id="@+id/c_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginStart="73dp"
android:layout_marginTop="69dp"
android:backgroundTint="#FFC107"
android:text="C"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="@+id/almacen"
app:layout_constraintTop_toTopOf="@+id/almacen" />

<Button
android:id="@+id/d_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginTop="2dp"
android:layout_marginEnd="73dp"
android:backgroundTint="#FFC107"
android:text="D"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/c_button"
app:layout_constraintEnd_toEndOf="@+id/almacen"
app:layout_constraintTop_toTopOf="@+id/c_button" />

<Button
android:id="@+id/e_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginTop="140dp"
android:backgroundTint="#FFC107"
android:text="E"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/d_button"
app:layout_constraintStart_toStartOf="@+id/d_button"
app:layout_constraintTop_toBottomOf="@+id/d_button" />

<Button
android:id="@+id/f_button"
android:layout_width="62dp"
android:layout_height="66dp"
android:layout_marginTop="140dp"
android:backgroundTint="#FFC107"
android:text="F"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="@+id/e_button"
app:layout_constraintStart_toStartOf="@+id/e_button"
app:layout_constraintTop_toBottomOf="@+id/e_button" />

</android.support.constraint.ConstraintLayout>

我做错了什么?我尝试了很多方法都没有解决问题。

最佳答案

您必须为所有 View 提供链,如下所示

<Button
android:id="@+id/button"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="C"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="B"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />

<Button
android:id="@+id/button3"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />

<Button
android:id="@+id/button4"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="D"
app:layout_constraintBottom_toTopOf="@+id/button5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button5"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="E"
app:layout_constraintBottom_toTopOf="@+id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toBottomOf="@+id/button4" />

<Button
android:id="@+id/button6"
android:layout_width="62dp"
android:layout_height="62dp"
android:backgroundTint="#FFC107"
android:text="F"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button3"
app:layout_constraintTop_toBottomOf="@+id/button5" />

enter image description here

关于java - 约束布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56153995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com