【问题标题】:Constraint layout does not center when wrap_content is used使用 wrap_content 时约束布局不居中
【发布时间】:2021-09-22 14:00:01
【问题描述】:

我为我的学习应用创建了一个简单的布局。但是,我的控件内部布局没有居中,实际上它非常偏离中心。你能帮忙吗?

<?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>

谢谢你, 安卓新手

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案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,而实际上里面的一个(控件)已经在上面关闭了。

    在你把/&gt;改错,把&gt;改错后,这个就不再红了,恢复正常了。

    【讨论】:

    • 谢谢马丁。看来我还没有 Android 眼睛 :)
    • 全在臀部,全在臀部 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2019-08-28
    相关资源
    最近更新 更多