【发布时间】:2020-10-11 20:39:06
【问题描述】:
是否有可能以某种方式沿ConstraintLayout 中的水平线动态定义 TextView 的点?
我有水平线,我有一些整数值(它可以从 0 到 40),如果它会更低,它应该位于更靠近这条水平线的左端,如果值会更高,则 textview应该更靠近这条水平线的右端。
现在我的 xml 布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
...
<TextView
android:id="@+id/lowestScoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/spacing_3xl"
android:layout_marginStart="@dimen/spacing_xl"
android:text="0"
...
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/firstLine"
app:layout_constraintTop_toBottomOf="@id/titleTextView"
app:layout_constraintHorizontal_chainStyle="packed" />
<View
android:id="@+id/firstLine"
android:layout_width="50dp"
android:layout_height="@dimen/spacing_2xs"
...
android:layout_marginStart="@dimen/spacing_2xs"
android:layout_marginEnd="@dimen/spacing_xs"
app:layout_constraintStart_toEndOf="@id/lowestScoreTextView"
app:layout_constraintEnd_toStartOf="@id/secondLine"
app:layout_constraintTop_toTopOf="@id/lowestScoreTextView"
app:layout_constraintBottom_toBottomOf="@id/lowestScoreTextView"
app:layout_constraintHorizontal_chainStyle="packed" />
<View
android:id="@+id/secondLine"
android:layout_width="100dp"
android:layout_height="@dimen/spacing_2xs"
...
android:layout_marginEnd="@dimen/spacing_xs"
app:layout_constraintStart_toEndOf="@id/firstLine"
app:layout_constraintEnd_toStartOf="@id/thirdLine"
app:layout_constraintTop_toTopOf="@id/lowestScoreTextView"
app:layout_constraintBottom_toBottomOf="@id/lowestScoreTextView"
app:layout_constraintHorizontal_chainStyle="packed" />
<View
android:id="@+id/thirdLine"
android:layout_width="50dp"
android:layout_height="@dimen/spacing_2xs"
...
android:layout_marginEnd="@dimen/spacing_2xs"
app:layout_constraintStart_toEndOf="@id/secondLine"
app:layout_constraintEnd_toStartOf="@id/highestScoreTextView"
app:layout_constraintTop_toTopOf="@id/lowestScoreTextView"
app:layout_constraintBottom_toBottomOf="@id/lowestScoreTextView"
app:layout_constraintHorizontal_chainStyle="packed" />
<TextView
android:id="@+id/highestScoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/spacing_xl"
android:text="40"
...
app:layout_constraintStart_toEndOf="@id/thirdLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/lowestScoreTextView"
app:layout_constraintBottom_toBottomOf="@id/lowestScoreTextView"
app:layout_constraintHorizontal_chainStyle="packed" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
【问题讨论】:
标签: android android-layout textview