如何在恒定间隔的约束布局中居中放置两个文本视图?

时间:2019-03-19 00:32:50

标签: android android-layout textview android-constraintlayout

我有一个用于RecyclerView的布局(省略了标准内容):

<androidx.constraintlayout.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TextView
    android:id="@+id/id_tv_movie_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@+id/id_tv_movie_release_year"
    app:layout_constraintStart_toStartOf="parent" />
<TextView
    android:id="@+id/id_tv_movie_release_year"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/id_tv_movie_name" />
</androidx.constraintlayout.ConstraintLayout>

但是当我添加很长和很短的标题时,我会得到:

enter image description here

彩色线显示每一行之间的确切距离 startOfParent-startOf1-endOf1-startOf2-endOf2-endOfParent

如何将这个textViews分成2个等宽的列,当标题较大时,将其分成更多行?

类似

  The Lord of The Rings:                        2001
The FellowShip of the Ring
           MIB                                  1997

3 个答案:

答案 0 :(得分:3)

您可以在下面做

text

enter image description here

答案 1 :(得分:2)

最简单的分割项目的方法是使每个TextView占用一个恒定的宽度,就是这样指定layout_constraintWidth_percent的宽度的50%:

app:layout_constraintWidth_percent="0.5"

要将宽度的70%分配给标题(居中),将30%的宽度分配给年份(右对齐),请执行以下操作:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/id_tv_movie_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="center"
        android:text="A Very, Very, Very, Very, Very, Very, Very, Very, Very Long Movie Title"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/id_tv_movie_release_year"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.7" />

    <TextView
        android:id="@+id/id_tv_movie_release_year"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="end"
        android:text="2002"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>

将显示以下内容:

enter image description here

如果不需要以50/50分割,则可以使用水平链和布局权重进行相同的布局。

答案 2 :(得分:0)

您可以使用的另一个简单选项是chains

Option Explicit

Sub Loomisinsert()

    With ThisWorkbook.Worksheets("Sheet1")

        'Deletes old Data and inserts new
        .Range("B2:J1000").Clear
        .Range("B5").Copy .Range("B2") '<- Easy way to copy - paste

        'Deletes unnecessary data
        .Range("K3:N5000,P3:Q5000,S3:U5000").Clear'<- Does not need to use "L3:L5000" because included in "K3:N5000". Additionally, have in mind that *.Delete* may cause problem with columns & rows. It s more preferable to use *.Clear* or *.Clearcontents*. *.Clear* used to clear both formats and value. In the other hand *.Cleatcontents* just empty the cell from value.

    End With

End Sub
相关问题