我为我的学习应用创建了一个简单的布局。但是,我的控件内部布局没有居中,实际上它非常偏离中心。你能帮忙吗?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gotta catch em all!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="18dp"
android:textSize="28sp"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/controls"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"/>
<EditText
android:id="@+id/search_pokemon_txt"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintTop_toTopOf="@+id/controls"
app:layout_constraintStart_toStartOf="@+id/controls"/>
<Button
android:id="@+id/search_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintTop_toTopOf="@+id/controls"
app:layout_constraintStart_toEndOf="@+id/search_pokemon_txt"
android:layout_marginStart="12dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
谢谢, 安卓新手
答案 0 :(得分:1)
问题是您关闭了没有项目的内部约束布局。
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/controls"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="32dp"/> <----- this should be just ">"
因此您的编辑文本和按钮当前位于内部布局之外。
如果您查看 Android Studio 中的最后两行,这也很明显:
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
一个是红色的,因为你关闭了两个 CL,而实际上里面的一个(控件)已经在上面关闭了。
在您纠正错误的 />
并输入正确的 >
后,这将不再是红色并恢复正常。