项目在 Android Studio 模拟器中的位置错误

Items are in wrong place in Android Studio emulator

我是 android 工作室的新手,这是我的第一个应用程序。 它工作正常,但模拟器有问题,它在错误的位置显示按钮。

<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.dell_pc.myapplication.MainActivity">

    <EditText
        android:id="@+id/txtyear"
        android:layout_width="193dp"
        android:layout_height="60dp"
        android:ems="10"
        android:hint="Enter year of birth"
        android:inputType="number"
        tools:layout_editor_absoluteY="129dp"
        tools:layout_editor_absoluteX="96dp" />

    <Button
        android:id="@+id/butage"
        android:layout_width="115dp"
        android:layout_height="41dp"
        android:onClick="BuGetAge"
        android:text="Show Age"
        tools:layout_editor_absoluteY="277dp"
        tools:layout_editor_absoluteX="135dp" />

</android.support.constraint.ConstraintLayout>

如何让它出现在正确的位置?

你绝对需要使用约束布局吗?如果没有,你可以使用相对布局,并使用 layout_centerHorizontal 和 layout_centerVertical 像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="blah blah"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="press"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/edittext"
        />

</RelativeLayout>

tools:layout_editor_absoluteY 和 tools 命名空间中的其他属性仅在布局编辑器中有效。您的工作代码不使用这些属性,因此您的视图位于左上角,因为这是默认设置。如上所述,RelativeLayout 甚至 LinearLayout 更适合这种简单的布局。

Select Component/Item(如BUTTON)在activity_main.xml -> 点击"Infer Constrains(as I show in attached file)"

您的项目将在 ASE 中正确放置

对其余的做同样的事情Component/Items

只需更改:

</android.support.constraint.ConstraintLayout>

</RelativeLayout>

问题解决了!开心点:D

单击一个元素,然后单击设计视图上方工具栏中的魔术棒图标以推断约束,这样该元素将不会在运行时跳转到 (0, 0) 位置。