android - 如何为两个 View 设置约束?

标签 android android-layout android-constraintlayout

所以我有一个这样的约束布局

<ConstraintLayout>
    <TextView
    android:id="@+id/text_view"
    app:layout_constraintEnd_toStartOf="@+id/view1" />

    <View
    android:id="@+id/view1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

    <View
    android:id="@+id/view2"
    aapp:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>

目前,textview 的结尾在 view1 的开头,但是,当 view1 的可见性消失时,我希望 textview 的结尾到 view2 的开头,否则长字符串可能会覆盖 view2。有没有办法将约束设置为两个 View ,或者有没有更好的解决方案?

最佳答案

您可以使用 Barrier 来约束多个 View 。

<ConstraintLayout>

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        app:barrierDirection="left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="view1,view2"/>    

    <TextView
        android:id="@+id/text_view"
        app:layout_constraintEnd_toStartOf="@id/barrier" />

    <View
        android:id="@+id/view1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <View
        android:id="@+id/view2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</ConstraintLayout>

TextView 将被约束到更左侧的 View

关于android - 如何为两个 View 设置约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50047796/

相关文章:

android - 为android开发时的文件结构

Android - <include layout=.../> 返回错误

android - 使用自定义 View 作为 RecyclerView 中的项目 - 约束布局无法处理具有匹配约束宽度的子项

android - constraintlayout.widget.Group 动画不适用于 TransitionManager

android - Angular/ ionic 结合慢

android - 当在 onHandleIntent 中创建处理程序时,使用处理程序发布的来自 IntentService 的 Toast 不显示

android - Python 和 Dalvik 或 AAF 之间的本地桥梁

android - CollapsingToolbarLayout 中 ImageView 的 OnClickListener

android - 突出显示可扩展列表中的选定项目

android - Fragments ConstraintLayout 是在 activity 的 FrameLayout 中截取的