动态功能模块中的 ConstraintLayout Barrier 失败

fik*_*r4n 7 android android-constraintlayout android-app-bundle

我有ConstraintLayout一个XML布局中,它包含了3次和Barrier,它们是button2textView2barrier2,和button3。正如预期的那样,button3成功地放置在button2和 下textView2,受使用 约束barrier2。然而,当在动态特征模块中使用时,它似乎没有引用约束视图(button2textView2),所以它坚持顶部。button3

这些屏幕截图显示它是成功的基础模块,但在动态功能模块中不起作用:

截图

基本功能动态功能中的 XML 布局如下所示:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView 2"
        app:layout_constraintStart_toEndOf="@id/button2"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="button2,textView2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/barrier2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用代码而不是 XML 设置约束,则成功:

  barrier2.referencedIds = intArrayOf(R.id.button2, R.id.textView2)
Run Code Online (Sandbox Code Playgroud)

如何正确引用button2textView2在 XML 布局中?

fik*_*r4n 2

检查(反编译的)源代码,我发现了一个(hacky?)解决方案:使用完全限定的资源 ID ( package:type/entry)。这似乎不是“官方方式”,因为 linter 给出了错误,但它有效。

假设应用程序包是com.example.app,动态功能模块名称是dynfeat,则在 ID 前面加上<package>.<module>:id/如下内容:

app:constraint_referenced_ids="com.example.app.dynfeat:id/button2,com.example.app.dynfeat:id/textView2"
Run Code Online (Sandbox Code Playgroud)

我其实对这个解决方案并不满意,因为这样的代码很难维护,例如在重命名动态功能模块名称时。因此,另一个解决方案是对Barrier类进行子类化并在构造函数中处理它。