android - 约束布局包含问题

标签 android xml android-layout android-constraintlayout

我正在使用 include 设置一些嵌套约束布局,类似于 this question但还没有得到答案。

约束没有像预期的那样起作用。

我已经尝试将 include xml 中的布局更改为 LinearLayout,但仍然无效。我已尝试添加 _bias 但仍然无法正常工作。

你能帮我解决这个问题吗?关于如何做到这一点有什么建议吗?

这是我的父布局

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <include android:id="@+id/mySearch"
        layout="@layout/layout_search"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/header"/>


    <include
        android:id="@+id/header"
        layout="@layout/layout_header_back_btn_and_title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/mySearch" />

</android.support.constraint.ConstraintLayout>

这是我的 layout_search.xml


<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/et_search"
        android:layout_width="0dp"
        android:layout_height="@dimen/size_50_dp"
        app:layout_constraintEnd_toEndOf="@id/btn_search"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_search"
        android:layout_width="50dp"
        android:layout_height="@dimen/size_50_dp"
        android:drawableEnd="@drawable/icon_search_40"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

layout_header_back_btn_and_title.xml


<android.support.constraint.ConstraintLayout
    ..>


    <ImageView
        android:id="@+id/btn_back"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/icon_arrow_left"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/tvTitlePage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="Keranjang"
        app:layout_constraintTop_toTopOf="@id/btn_back"
        app:layout_constraintStart_toStartOf="@id/btn_back"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

我希望后退按钮和标题应该在搜索布局下方。 但随后它反而重叠了。

[ Expected layout ]预计1

[ Actual layout ]实际2

最佳答案

无论何时使用 <include>,您都需要提供包含的布局的大小在你的布局中。像在各自的布局中一样设置它的高度和宽度,它应该可以正常工作。

将您的父级布局更改为此,它应该可以解决您的问题。

<android.support.constraint.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto">

<include android:id="@+id/mySearch"
    layout="@layout/layout_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/header"/>


<include
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    layout="@layout/layout_header_back_btn_and_title"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/mySearch" />

</android.support.constraint.ConstraintLayout>

关于android - 约束布局包含问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57960263/

相关文章:

android - Android Studio 3.0 上的 com.android.dex.DexIndexOverflowException

android - 如何在 Travis 中显示 HTML 格式的输出文件?

Android:ActionBarSherlock 搜索小部件

android - 如何在适配器类中维护 recyclerview 项目的可见性

安卓 : Insert multiple rows into sqlite database not working

java - 创建线程会导致 Android 应用程序强制关闭

xml - 如何使用 XSL 转义 XML 内容以将其安全地输出为 JSON?

引用对象的 C# 序列化

c# - 如何从 XML 中获取值?

android - 如何使用数组列表中的数据填充自定义布局 ListView