【问题标题】:Dynamic spot of TextView within ConstraintLayoutConstraintLayout内TextView的动态点
【发布时间】: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


    【解决方案1】:

    使用ConstraintSet 以编程方式为您的TextView 提供水平Bias

    ConstraintSet setWithBias = new ConstraintSet();
    setWithBias.clone(constraintLayout); // You'd require a reference to this ConstraintLayout through findViewById()
    setWithBias.setHorizontalBias(R.id.OfYourTextView, 0.2); // 20% horizontal bias (0 - extreme left, 1 - extreme right)
    setWithBias.applyTo(constraintLayout); // Apply changes to your ConstraintLayout
    

    请注意,您的TextView 的左右边缘必须连接到适当的极端,这样才能正常工作。 根据您的代码:firstLine 的左边缘和thirdLine 的右边缘。

    如果你不设置偏差,它会在上面的约束下停留在中间,这隐含地表明bias 是 0.5

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 2018-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2020-02-27
      • 1970-01-01
      相关资源
      最近更新 更多