我是 Android Studio
的新手,当我创建我的应用程序时,我的所有元素都相互重叠。我不确定如何编辑此代码。请给我一个详细的描述。
布局编辑器和模拟器结果
布局代码
<?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 重叠时出现奇怪的行为(因为上面提到的约束没有在应用程序代码中使用,我们可以推断根本没有约束)。
由于您是初学者,我建议您从 LinearLayout
或 RelativeLayout
开始,因为我认为 ConstraintLayout
更难理解,请使用并调试。
关于android - 在我的 android studio 模拟器中元素相互重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45145804/