我的按钮和文本显示在Android Studio 4.0的错误位置

问题描述

嗨,我正在练习可点击按钮,为此,我需要在此处复制设计: https://www.figma.com/file/pAx28mFg68qvYulJQcyMHE/Score-Tracker?node-id=0%3A1

但是出于某种原因,尽管我相信我正确地编写了所有尺寸,但是我无法将按钮和零位放置在应该放置的确切位置,我也不知道为什么!

这是我的xml代码。您能帮我找出我设定的尺寸有什么问题吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/background">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/background"/>
            <TextView
                android:id="@+id/numberOfGoals"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="163dp"
                android:layout_marginTop="124dp"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <TextView
                android:id="@+id/numberOfGoals2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="163dp"
                android:layout_alignTop="@id/Button2"
                android:layout_marginBottom="124dp"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <Button
                android:id="@+id/Button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginTop="14dp"
                android:backgroundTint="#EB5757"
                android:layout_alignParentTop="true"/>
            <Button
                android:id="@+id/Button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:backgroundTint="#EB5757"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="14dp"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

解决方法

也许这会有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@drawable/background">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/background"/>

            <TextView
                android:id="@+id/numberOfGoals"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/Button1"
                android:layout_centerHorizontal="true"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <TextView
                android:id="@+id/numberOfGoals2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/Button2"
                android:layout_centerHorizontal="true"
                android:text="0"
                android:textColor="#FFFFFF"
                android:textSize="72sp"/>
            <Button
                android:id="@+id/Button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginTop="14dp"
                android:backgroundTint="#EB5757"
                android:layout_alignParentTop="true"/>
            <Button
                android:id="@+id/Button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Goal"
                android:backgroundTint="#EB5757"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="14dp"
                android:layout_alignParentBottom="true"/>
        </RelativeLayout>
    </LinearLayout>

</RelativeLayout>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...