【问题标题】:How to layout 2 textviews in a custom compound constraint layout如何在自定义复合约束布局中布局 2 个文本视图
【发布时间】:2019-03-29 08:35:49
【问题描述】:

我试图在我的布局文件的 Constraintlayout 中为两个 Textview 提供正确的位置。问题是文本视图需要使用应用程序进行扩展:autoSizeTextType="uniform"。为了使它们扩展,我还必须在layout_widthlayout_height 上匹配_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>

实际结果:

两个文本视图都缩放,但重叠,并放置在约束布局的中心。 当前状态:

预期结果:

一个文本视图在中间居中。一个文本视图在底部对齐。两个文本视图都与约束布局一起缩放。 预期结果:(活动中有 3 种不同的自定义化合物)

【问题讨论】:

  • 您需要为 textview 使用固定高度,而不是 match_parent 才能自动调整大小。

标签: android xml textview android-constraintlayout


【解决方案1】:

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

 <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>

【讨论】:

  • 这就像一个魅力!唯一缺少的是底部的textview应该比上面的小。我怎样才能做到这一点?比如 75/25 或 60/30 等等....
  • Nvm。这是使用 autoSizeStepGranulairty 修复的!谢谢!
【解决方案2】:

这可能会有所帮助

<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>

我们必须添加的Textview大小

【讨论】:

    【解决方案3】:

    将 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" />
    

    【讨论】:

      猜你喜欢
      • 2019-11-24
      • 1970-01-01
      • 2019-05-10
      • 2020-06-09
      • 1970-01-01
      • 2013-01-22
      • 2018-04-18
      • 1970-01-01
      • 2014-11-02
      相关资源
      最近更新 更多