ConstraintLayout左对齐两个水平链的中间元素?

时间:2018-03-01 09:58:03

标签: android android-constraintlayout constraint-layout-chains

我正在创建一个布局,我需要创建两行,每行三个元素。对于两行的中间元素(Qty4901),我希望文本保持对齐。

enter image description here

我创建了2个水平链,chainStylespread_inside

这是布局xml:

https://gist.github.com/asheshb/cb2effdbb92e34d897672eb730339896

知道怎么做吗?

1 个答案:

答案 0 :(得分:1)

尝试下面的布局虽然我不推荐这种方法,因为如果你在TextView中插入一个值太长的TextView(例如Curr.Value),只附加了元素(在这种情况下是包含这个的TextView)数字15,13,​​184)将适应这些变化,而且布局将被破坏。

<android.support.constraint.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:id="@+id/nse_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">

<TextView
    android:id="@+id/tvStockName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="ICICI BANK LIMITED" />

<TextView
    android:id="@+id/tvStockSymbol"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    app:layout_constraintStart_toStartOf="@+id/tvStockName"
    app:layout_constraintTop_toBottomOf="@+id/tvStockName"
    tools:text="ICICIBANK" />

<TextView
    android:id="@+id/tvStockValue"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="end"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="306.75" />

<TextView
    android:id="@+id/tvStockPercent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="2dp"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvStockValue"
    tools:text="(1.44%)"
    />


<TextView
    android:id="@+id/tvStockDifference"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:textSize="16sp"
    app:layout_constraintEnd_toStartOf="@+id/tvStockPercent"
    app:layout_constraintTop_toBottomOf="@+id/tvStockValue"
    tools:text="-4.50"
    />

<TextView
    android:id="@+id/tvAvgPriceTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:textSize="16sp"
    tools:text="Avg. Price"
    app:layout_constraintHorizontal_weight="50"
    app:layout_constraintEnd_toStartOf="@+id/tvQtyTitle"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvStockDifference" />

<TextView
    android:id="@+id/tvQtyTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:textSize="16sp"
    tools:text="Qty"
    app:layout_constraintHorizontal_weight="10"
    app:layout_constraintEnd_toStartOf="@+id/tvCurrentValueTitle"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/tvAvgPriceTitle"
    app:layout_constraintTop_toBottomOf="@+id/tvStockDifference" />

<TextView
    android:id="@+id/tvCurrentValueTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    tools:text="Curr. Value"
    android:textSize="16sp"
    app:layout_constraintHorizontal_weight="20"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/tvQtyTitle"
    app:layout_constraintTop_toBottomOf="@+id/tvStockDifference" />


<TextView
    android:id="@+id/tvAvgPrice"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    app:layout_constraintHorizontal_weight="50"
    app:layout_constraintEnd_toStartOf="@+id/tvQty"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintHorizontal_chainStyle="spread_inside"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvAvgPriceTitle"
    tools:text="241.83" />

<TextView
    android:id="@+id/tvQty"
    android:layout_width="0dp"
    android:layout_height="20dp"
    app:layout_constraintHorizontal_weight="10"
    app:layout_constraintEnd_toStartOf="@+id/tvTotalStockInvestment"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toEndOf="@+id/tvAvgPrice"
    app:layout_constraintTop_toBottomOf="@+id/tvQtyTitle"
    tools:text="4901" />

<TextView
    android:id="@+id/tvTotalStockInvestment"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize="16sp"
    app:layout_constraintHorizontal_weight="20"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/tvQty"
    app:layout_constraintTop_toBottomOf="@+id/tvCurrentValueTitle"
    tools:text="15,13,184" />


<TextView
    android:id="@+id/tvTotalStockPercent"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:textSize="12sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvTotalStockInvestment"
    tools:text="(+27.69%)"
    />


<TextView
    android:id="@+id/tvTotalStockDifference"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    android:textSize="14sp"
    app:layout_constraintEnd_toStartOf="@+id/tvTotalStockPercent"
    app:layout_constraintTop_toBottomOf="@+id/tvTotalStockInvestment"
    tools:text="+3,28,234"
    />

<TextView
    android:id="@+id/tvDayStockDifference"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:textSize="14sp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvTotalStockDifference"
    tools:text="-24,015"
    />


<TextView
    android:id="@+id/tvTitleDayStockDifference"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    tools:text="Today's Gain"
    android:textSize="14sp"
    app:layout_constraintEnd_toStartOf="@+id/tvDayStockDifference"
    app:layout_constraintTop_toBottomOf="@+id/tvTotalStockDifference" />

上面布局的输出:

Output of the layout above