我正在创建一个android应用程序,但按钮没有排列在我放在设计器中的位置

我正在创建一个android应用程序,但按钮没有排列在我放在设计器中的位置,android,android-layout,android-studio,Android,Android Layout,Android Studio,我正在尝试制作我的第一个Android应用程序,它允许我增加或减少显示的数字。我的问题是,当我在Android Studio中查看应用程序的外观时,它看起来很好,但是,当我在上运行应用程序时,按钮都位于屏幕的左上角。我的项目支持Android 4.0。这可能很简单,但由于这是我的第一个应用程序,我真的不知道从哪里开始。下面是我的项目的XML代码。感谢您事先提供的任何帮助/建议 <?xml version="1.0" encoding="utf-8"?> <android.sup

我正在尝试制作我的第一个Android应用程序,它允许我增加或减少显示的数字。我的问题是,当我在Android Studio中查看应用程序的外观时,它看起来很好,但是,当我在上运行应用程序时,按钮都位于屏幕的左上角。我的项目支持Android 4.0。这可能很简单,但由于这是我的第一个应用程序,我真的不知道从哪里开始。下面是我的项目的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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tyler.mtglifecounter.MainActivity">

<TextView
    android:id="@+id/lifeTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="20"
    android:textAlignment="center"
    android:textSize="70sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.184" />

<Button
    android:id="@+id/plusOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+ 1"
    android:textSize="20sp"
    tools:layout_editor_absoluteX="82dp"
    tools:layout_editor_absoluteY="230dp"/>

<Button
    android:id="@+id/minusOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="- 1"
    android:textSize="20sp"
    tools:layout_editor_absoluteX="213dp"
    tools:layout_editor_absoluteY="230dp" />

<Button
    android:id="@+id/plusFive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+ 5"
    android:textSize="20sp"
    tools:layout_editor_absoluteX="82dp"
    tools:layout_editor_absoluteY="330dp" />

<Button
    android:id="@+id/minusfive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="- 5"
    android:textSize="20sp"
    tools:layout_editor_absoluteX="213dp"
    tools:layout_editor_absoluteY="330dp" />

<Button
    android:id="@+id/reset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset"
    android:textSize="14sp"
    tools:layout_editor_absoluteX="148dp"
    tools:layout_editor_absoluteY="429dp" />



您的问题是编辑器添加的
工具:布局编辑器绝对
工具:布局编辑器绝对
属性,它们在编译的应用程序中没有实际效果。删除这些元素并尝试使用文本视图(由xml预览而不是设计视图引导)放置元素


如果您刚开始使用Android,我建议您尝试使用更简单的布局,例如
RelativeLayout

您的问题是
工具:布局编辑器\u absoluteX
工具:布局编辑器\u absoluteY
编辑器添加的属性,它们在编译的应用程序中没有实际效果。删除这些元素并尝试使用文本视图(由xml预览而不是设计视图引导)放置元素


如果您刚开始使用Android,我建议您尝试使用更简单的布局,比如
RelativeLayout
Yo tmoran
ConstraintLayout
最近才成为
Android Studio
的默认布局,如果不了解底层架构是如何工作的,这是非常困难的

tools:layout\u editor\u absoluteX
tools:layout\u editor\u absoluteY
值仅应用于编辑器中的定位,我不确定为什么会存在这些值,因为它们也曾让我感到困惑。这就是为什么它在
设计视图中看起来正确,但在设备上却不正确

要对齐按钮,需要使用约束,类似于使用以下命令对齐
TextView

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

这是在屏幕中部的“代码>文本视图< /代码>,然后行:

app:layout\u constraintVertical\u bias=“0.184”

为按钮提供0.184的垂直偏差。0.5将直接在中间,所以这是移动它(0.5 - 0.184=)31.6%页。

因此,为了执行您想要的操作,首先使用以下命令将底部的
按钮约束到父级:

app:layout\u constraintBottom\u toBottomOf=“parent”

(这样做的目的是将
按钮的底部限制在页面的底部)然后您可能希望给它一个边距,以便将其设置为离底部那么远:

android:layout_marginBottom=16dp

然后将其水平居中添加:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
您可以在不键入代码的情况下执行上述操作,您需要单击按钮的圆形边缘,然后将它们拖动到布局中的相关位置,以便它们单击到位您不希望只单击并拖动按钮,因为这只会导致
设计视图
发生更改

将接下来的两组按钮放在正确的位置更加复杂。看一看。正如前面的回答所说,
ConstraintLayout
非常先进,
RelativeLayout
更容易,但是我认为,如果你深入研究,并找出如何使用
,它将带来好处,因为这将使它在未来变得更容易


您需要首先将最后一行按钮约束到底部按钮,然后使用相同的方法将第一行按钮约束到第二行。

Yo tmoran
ConstraintLayout
最近才成为
Android Studio
的默认布局,如果不了解底层架构是如何工作的,这是非常困难的

tools:layout\u editor\u absoluteX
tools:layout\u editor\u absoluteY
值仅应用于编辑器中的定位,我不确定为什么会存在这些值,因为它们也曾让我感到困惑。这就是为什么它在
设计视图中看起来正确,但在设备上却不正确

要对齐按钮,需要使用约束,类似于使用以下命令对齐
TextView

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"

这是在屏幕中部的“代码>文本视图< /代码>,然后行:

app:layout\u constraintVertical\u bias=“0.184”

为按钮提供0.184的垂直偏差。0.5将直接在中间,所以这是移动它(0.5 - 0.184=)31.6%页。

因此,为了执行您想要的操作,首先使用以下命令将底部的
按钮约束到父级:

app:layout\u constraintBottom\u toBottomOf=“parent”

(这样做的目的是将
按钮的底部限制在页面的底部)然后您可能希望给它一个边距,以便将其设置为离底部那么远:

android:layout_marginBottom=16dp

然后将其水平居中添加:

app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
您可以在不键入代码的情况下执行上述操作,您需要单击按钮的圆形边缘,然后将它们拖动到布局中的相关位置,以便它们单击到位您不希望只单击并拖动按钮,因为这只会导致
设计视图
发生更改

将接下来的两组按钮放在正确的位置更加复杂。看一看。正如前面的回答所说,
ConstraintLayout
非常先进,而且
RelativeLayout