android - 在我的 android studio 模拟器中元素相互重叠

标签 android emulation

我是 Android Studio 的新手,当我创建我的应用程序时,我的所有元素都相互重叠。我不确定如何编辑此代码。请给我一个详细的描述。

布局编辑器和模拟器结果

enter image description here

布局代码

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="schemas.android.com/apk/res/android"; 
    xmlns:app="schemas.android.com/apk/res-auto"; 
    xmlns:tools="schemas.android.com/tools"; 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.first.anew.MainActivity"> 

    <TextView
        android:layout_width="216dp"
        android:layout_height="60dp"
        android:text="Hello World!"
        tools:layout_editor_absoluteY="16dp"
        tools:layout_editor_absoluteX="61dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="100dp"
        android:layout_height="45dp"
        android:text="Login"
        tools:layout_editor_absoluteY="343dp"
        tools:layout_editor_absoluteX="177dp" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="215dp"
        android:layout_height="65dp"
        android:text="username"
        tools:layout_editor_absoluteY="117dp"
        tools:layout_editor_absoluteX="61dp" />

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline"
        app:layout_constraintGuide_begin="20dp"
        android:orientation="horizontal" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="217dp"
        android:layout_height="57dp"
        android:ems="10"
        android:inputType="textPassword"
        tools:layout_editor_absoluteY="218dp"
        tools:layout_editor_absoluteX="61dp" />
</android.support.constraint.ConstraintLayout>

最佳答案

您的模拟器屏幕和布局编辑器因 tools 命名空间而不同。简而言之,它是一个帮助您更好地可视化编写的代码的工具。此工具还会生成 tools:layout_editor_absoluteY="343dp" 等约束,通知布局编辑器应如何绘制 View 。问题是这个约束不会出现在你的应用程序代码中,导致当所有 View 重叠时出现奇怪的行为(因为上面提到的约束没有在应用程序代码中使用,我们可以推断根本没有约束)。

由于您是初学者,我建议您从 LinearLayoutRelativeLayout 开始,因为我认为 ConstraintLayout 更难理解,请使用并调试。

关于android - 在我的 android studio 模拟器中元素相互重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45145804/

相关文章:

android - 加载器可以在没有内容提供者的情况下使用还是必须结合使用?

android - 在 Android 2.3 上使用 JQuery Mobile 的 Phonegap 中缺少转换

java - Android:当我运行该应用程序时,它崩溃了

android - Xamarin.Forms-RelativeLayout 内的约束

java - Android 可以接听空中来电吗?

android - 广播怎么可能是 "malicious"?

ios - 无法启动 Cordova iOS 模拟器 - Mac 上的 PhoneGap

assembly - 游戏男孩 : Half-carry flag and 16-bit instructions (especially opcode 0xE8)

java - Android - 将图像从 URL 保存到 SD 卡

android - 有没有办法在 Windows 程序中模拟 Android 应用程序?