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

如何使视图在约束布局中居中而不重叠不均匀的邻居

  •  0
  • Andy  · 技术社区  · 7 年前

    我正在为我的标题栏使用自定义布局,基于 ConstraintLayout . 我需要中心的标题文本,而不是重叠的按钮在两边。目前我把标题居中放在侧按钮之间,但是由于它们的宽度不同,标题没有在父视图中居中。

    Current title bar layout

    1 回复  |  直到 7 年前
        1
  •  2
  •   Jasurbek    7 年前

    要使视图居中,您可以在将视图的两侧连接到其他视图的同时,给布局“u width=”0dp“。然后中心视图占据了与视图左侧相同的位置

    更新

    如果你想把中心放在父母身上, 其他人的看法 会是你的父母。

    android:layout_width = "0dp"
    android:layout_height = "wrap_content"
    android:ellipsize = "end"
    android:maxLines = "1"
    app:layout_constraintEnd_toEndOf = "parent"
    app:layout_constraintStart_toStartOf = "parent"
    
        2
  •  0
  •   Udara Abeythilake    7 年前

    你可以用

    1. 应用:布局图
    2. 应用程序:布局\u constraintStart_to start of

    如下图所示

    <TextView android:id="@+id/nomination" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    app:layout_constraintTop_toTopOf="@+id/someId1" 
    app:layout_constraintBottom_toBottomOf="@+id/someId2" 
    app:layout_constraintStart_toStartOf="@+id/awards" 
    app:layout_constraintEnd_toEndOf="parent"/>
    
        3
  •  0
  •   Shrikant    7 年前

    你应该从中间开始,然后向左和向右走。

    尝试下面的解决方案,它应该如预期的那样工作

    <android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:id="@+id/btn_at_the_start"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:gravity="center"
                android:text="Awards"
                app:layout_constraintEnd_toStartOf="@+id/tv_in_the_middle"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_percent="0.2" />
    
            <TextView
                android:id="@+id/tv_in_the_middle"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:ellipsize="end"
                android:gravity="center"
                android:textAlignment="center"
                android:padding="5dp"
                android:maxLines="2"
                android:text="Nominations"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_percent="0.6" />
    
            <ImageButton
                android:id="@+id/ibtn_at_the_end"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:background="@android:color/transparent"
                android:gravity="center_horizontal"
                android:src="@drawable/ic_add_white"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/tv_in_the_middle"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintWidth_percent="0.2" />
        </android.support.constraint.ConstraintLayout>