如何在自定义复合约束布局中布局2个文本视图

时间:2019-03-29 08:35:49

标签: android xml textview android-constraintlayout

我试图在布局文件的Constraintlayout中为两个Textview提供正确的位置。问题是文本视图需要使用以下应用程序进行扩展:autoSizeTextType="uniform"。为了使它们缩放,我还必须在layout_widthlayout_height上使用match_parent。这使两个文本视图重叠。我怎样才能达到预期的结果?

当前XML文件:

<?xml version="1.0" encoding="utf-8"?>

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center"
    android:orientation="vertical">


    <TextView

        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="1"
        android:textColor="#4689C8"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.362" />

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="ABC"
        android:textColor="#4689C8"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

实际结果:

两个textview都缩放但重叠,并放置在约束布局的中心。 当前状态:
The current state

预期结果:

一个textview位于中间。一个textview在底部对齐。两种文本视图均以constraintlayout缩放。 预期结果:(一个活动中有3种不同的自定义化合物) Expected result

3 个答案:

答案 0 :(得分:0)

您必须给顶部和底部适当的约束

 <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:gravity="center"
        android:orientation="vertical">

    <TextView
            android:id="@+id/title"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="1"
            android:textColor="#4689C8"
            android:textStyle="bold"
            app:autoSizeTextType="uniform"
            app:layout_constrainedHeight="true"
            app:layout_constraintBottom_toTopOf="@+id/message"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

    <TextView
            android:id="@+id/message"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center"
            android:text="ABC"
            android:textColor="#4689C8"
            android:textStyle="bold"
            app:autoSizeTextType="uniform"
            app:layout_constrainedHeight="true"
            app:layout_constraintTop_toBottomOf="@+id/title"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

答案 1 :(得分:0)

可能会有所帮助     

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">


    <TextView

        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="24dp"
        android:gravity="center"
        android:text="1"
        android:textSize="50sp"
        android:textColor="#4689C8"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center"
        android:text="ABC"
        android:textColor="#4689C8"
        android:textSize="28sp"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title" />

</android.support.constraint.ConstraintLayout>

我们必须添加的文本视图大小

答案 2 :(得分:0)

修改TextView的高度和宽度为0dp,这将与父约束视图匹配,为视图之间的关系添加约束,并且应该按您的要求提供

<TextView

        android:id="@+id/title"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="1"
        android:textColor="#4689C8"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toTopOf="@id/message"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

<TextView
        android:id="@+id/message"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="ABC"
        android:textColor="#4689C8"
        android:textStyle="bold"
        app:autoSizeTextType="uniform"
        app:layout_constrainedHeight="true"
        app:layout_constraintTop_toBottomOf="@id/title"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />