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

时间:2017-04-30 22:50:39

标签: android android-layout android-studio

所以我对Android开发完全陌生,我正在阅读一本关于Android游戏开发的书。我们建立的第一款游戏名为Tappy Defender。

无论如何,他们为我提供了背景图片并告诉我放一个按钮和一个TextView。我将背景放在drawable文件夹中并将其分配给Activity的背景,我还将TextView和按钮对齐。

但是,当我在模拟器中构建并运行程序时,按钮和TextView都位于左上角(横向)并且彼此重叠。背景也根本没有显示。

我很困惑,因为我做错了,因为我正确地遵循了这些步骤,并且已经过了好几次。

任何帮助表示感谢。

我也包含了.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>

1 个答案:

答案 0 :(得分:1)

您使用的是Constraint布局,但不限制任何内容。此外,您使用的“工具”属性仅适用于模拟器,在构建应用程序时会被忽略。

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

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

请阅读Constraint布局(https://developer.android.com/training/constraint-layout/index.html)并对视图应用约束。