我是新来的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"
,它通知布局编辑器应如何绘制视图。问题是,此约束不会出现在您的应用程序代码中,从而导致当所有视图重叠时出现奇怪的行为(由于应用程序代码中未使用上述约束,我们可以推断根本没有约束)。
由于您是初学者,我建议您从LinearLayout
or RelativeLayout
因为我认为ConstraintLayout
更难理解、使用和调试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)