代码之家  ›  专栏  ›  技术社区  ›  Christian

如何在包含宽度为“0dp”的视图的ConstraintLayout中使打包链居中?

  •  0
  • Christian  · 技术社区  · 6 年前

    我想把我的页面标题居中。标题看起来像 (1) Dokument1 (3) GreatDocument . 我想有右边和左边的空白 60dp wrap_content 对于 @+id/title 0dp 边距得到尊重,但居中不起作用。

    <android.support.constraint.ConstraintLayout
        android:id="@+id/top_bar_xml"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/open_brackets"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="("
            android:gravity="center_vertical"
            android:layout_marginStart="60dp"
    
            android:textSize="20sp"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintEnd_toStartOf="@id/page_count"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/page_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:gravity="center_vertical"
            android:textSize="20sp"
            app:layout_constraintStart_toEndOf="@id/open_brackets"
            app:layout_constraintEnd_toStartOf="@id/close_brackets"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/close_brackets"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=") "
            android:gravity="center_vertical"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintStart_toEndOf="@id/page_count"
            app:layout_constraintEnd_toStartOf="@id/title"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="60dp"
            android:text="DocumentTitle"
            android:textSize="20sp"
            android:gravity="center_vertical"
            android:ellipsize="end"
            app:layout_constraintStart_toEndOf="@+id/close_brackets"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>
    

    (我简化了这个例子,去掉了对ViewModels的页面计数和标题引用;文档下面有其他视图,这些视图的边距较低)

    1 回复  |  直到 6 年前
        1
  •  4
  •   Pawel Laskowski    6 年前

    您可以设置宽度 @id/title wrap_content

    app:layout_constrainedWidth="true"
    

    属性,以便 TextView 当它变得太大而无法满足利润率时,我们会尊重它。