【问题标题】:How to achieve the following layout with dynamic width in Constraint Layout?如何在Constraint Layout中实现如下动态宽度的布局?
【发布时间】:2019-11-14 18:48:00
【问题描述】:

我有一个约束布局,里面有一个简单的卡片视图,里面有一个文本,底部有一个文本。我希望卡片的宽度至少是底部文本的宽度。所以像设置最小宽度一样。

我画了一张图片来说明我想要的,但无法在约束布局中实现它。

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    Barriers 将为您施展魔法!
    使用此功能,您可以自动获取特定范围内最宽对象的宽度。

    来自 Android 文档:

    Barrier 引用多个小部件作为输入,并根据指定一侧的最极端小部件创建虚拟指南。例如,左屏障将与所有引用视图的左侧对齐。

    以下 xml 将检查视图 content_text 和 bottom_text 的最宽宽度

        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="content_text,bottom_text" />
    

    现在您可以将此障碍用作任何其他视图中的约束(在同一个 ConstraintLayout 内)

        <View
            android:id="@+id/card_background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="@id/content_text"
    
            app:layout_constraintEnd_toEndOf="@id/barrier"
    
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/content_text" />
    

    这就是我的结果:

    示例 1

    示例 2

    我的完整 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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="16dp"
        tools:context=".MainActivity">
    
        <View
            android:id="@+id/card_background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@color/colorPrimary"
            app:layout_constraintBottom_toBottomOf="@id/content_text"
    
            app:layout_constraintEnd_toEndOf="@id/barrier"
    
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/content_text" />
    
        <TextView
            android:id="@+id/content_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:paddingTop="16dp"
            android:paddingBottom="16dp"
    
            android:text="This is a very large content text. It is so long that it needs a second line"
            android:textColor="@android:color/black"
            android:textSize="22sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/bottom_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a bottom text with a particular length"
            android:textColor="@android:color/black"
            android:textSize="14sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/card_background" />
    
    
        <!-- Magic happens here -->
        <androidx.constraintlayout.widget.Barrier
            android:id="@+id/barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="end"
            app:constraint_referenced_ids="content_text,bottom_text" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    希望这会有所帮助。
    Here could you read more about barriers

    【讨论】:

    • 很棒的答案!如果 content_text 位于 CardView 之类的内部,这似乎无法实现?视图都必须是兄弟姐妹吧?
    • 是的,这个解决方案似乎只适用于视图兄弟。您可以通过将可见性设置为不可见并在卡片视图@+id/card_background 中插入相同的TextView(只有不同​​的id 和可见性设置为可见)将@+id/content_text 用作幻影视图,但我不建议这样做。
    • 您还可以编写自定义布局并以编程方式处理它。看看这个:guides.codepath.com/android/defining-custom-views
    猜你喜欢
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2019-02-10
    • 2018-03-08
    • 2023-01-13
    相关资源
    最近更新 更多