首页 > 解决方案 > 无法对包含的布局元素施加约束

问题描述

在我的 android 应用程序中,我试图拥有一个通用布局,其中包含一些可以在多个布局中重用的基本组件。但是,它不起作用。

这是我常用的布局,“common_elements.xml”:

<?xml version="1.0" encoding="utf-8"?>

<merge xmlns:android="http://schemas.android.com/apk/res/android">

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


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/horizontalGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/verticalGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />


    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/horizontalGuideline"
        app:layout_constraintLeft_toLeftOf="@+id/verticalGuideline"
        app:layout_constraintRight_toRightOf="@+id/verticalGuideline"
        app:layout_constraintTop_toTopOf="@+id/horizontalGuideline" />


</androidx.constraintlayout.widget.ConstraintLayout>

</merge>

我正在尝试包含这样的 common_elements:

<?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=".TL">

    <include android:id="@+id/horizontalGuideline" layout="@layout/common_elements" />
<!-- I have tried including an individual component as well as whole layout. 
    Both of these don't work.
    -->

    <include layout="@layout/common_elements"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />


    <TextView
        android:id="@+id/promptLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter"
        app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
        app:layout_constraintTop_toBottomOf="@+id/horizontalGuideline"
        app:layout_constraintLeft_toRightOf="@+id/verticalGuideline"
        app:layout_constraintRight_toLeftOf="@+id/verticalGuideline"
        android:layout_marginLeft="12dp"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

观察到的行为

在通用布局中,我有诸如指南和进度条之类的东西,我不想在每个布局文件中定义它们。在新布局中,我试图根据包含的布局中定义的准则设置约束。

但是,新布局中的元素不遵守这些约束,而是跳转到屏幕的开头。

我尝试了一些解决方案,比如包含合并标签,为包含的布局提供高度/宽度,只包含一个组件而不是整个布局。

这些都不起作用。

我究竟做错了什么?

标签: androidandroid-layout

解决方案


推荐阅读