模拟器中的布局与Android Studio的布局不匹配



所以我是Android开发的新手,我正在浏览有关Android游戏开发的书。我们构建的第一场比赛称为Tappy Defender。

无论如何,他们为我提供背景图像,并告诉我放一个按钮和一个文本视图。我将背景放在可绘制的文件夹中,并将其分配给活动的背景,我还对文本视图和按钮进行了对齐。

但是,当我在模拟器中构建和运行程序时,按钮和文本视图都位于左上角(以风景为单位(,并且彼此重叠。背景也根本没有显示。

我很困惑,因为我正确地遵循这些步骤并越过了几次,我做错了什么。

任何帮助。

我也包括.xml文件:

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible"
    tools:background="@drawable/background"
    tools:context="com.example.tappydefender.MainActivity">
    <Button
        android:id="@+id/buttonPlay"
        android:layout_width="122dp"
        android:layout_height="39dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="Play"
        tools:layout_editor_absoluteY="293dp"
        tools:layout_editor_absoluteX="280dp" />
    <TextView
        android:id="@+id/textHighScore"
        android:layout_width="158dp"
        android:layout_height="28dp"
        android:text="TextView"
        tools:layout_editor_absoluteX="264dp"
        tools:layout_editor_absoluteY="258dp"
        tools:text="High Score: 99999" />
</android.support.constraint.ConstraintLayout>

您使用的是约束布局,但是您没有约束任何内容。此外,您正在使用仅适用于模拟器的"工具"属性,并且在构建应用时被忽略。

您需要对视图设置约束,例如

app:layout_constrainTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/otherView"

请阅读约束布局(https://developer.android.com/training/constraint-layout/index.html(,并将约束应用于您的视图。

最新更新