android – 如何使用ConstraintLayout水平对齐三个TextView

android – 如何使用ConstraintLayout水平对齐三个TextView,第1张

概述我正在尝试水平对齐三个TextView(如标题所示),但即使将它们链接在一起并使用app:layout_constrainedWidth =“true”它们仍然被推出屏幕,奇怪的是只有左侧时中间或最后一个变大.请注意,我正在设置 android:maxLines =“1”和android:ellipsize =“end”并在屏幕的开头对齐它们,但不确定它是否重要. 我也试图限制它们的大小,但是在有 我正在尝试水平对齐三个TextVIEw(如标题所示),但即使将它们链接在一起并使用app:layout_constraineDWIDth =“true”它们仍然被推出屏幕,奇怪的是只有左侧时中间或最后一个变大.请注意,我正在设置 android:maxlines =“1”和androID:ellipsize =“end”并在屏幕的开头对齐它们,但不确定它是否重要.

我也试图限制它们的大小,但是在有两个小文本和一个非常大的文本的情况下,即使布局仍然留有一些空间,大的文本也将被椭圆化.

样本布局:

<?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="@androID:color/white"        androID:padding="22dp">        <TextVIEw            androID:ID="@+ID/greenTextVIEw"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_marginEnd="8dp"            androID:ellipsize="end"            androID:maxlines="1"            androID:textcolor="@androID:color/holo_green_dark"            app:layout_constraineDWIDth="true"            app:layout_constraintEnd_toStartOf="@ID/blueTextVIEw"            app:layout_constraintHorizontal_bias="0.0"            app:layout_constraintHorizontal_chainStyle="packed"            app:layout_constraintStart_toStartOf="parent"            app:layout_constrainttop_totopOf="parent"            tools:text="TextVIEw" />        <TextVIEw            androID:ID="@+ID/blueTextVIEw"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_marginEnd="8dp"            androID:ellipsize="end"            androID:maxlines="1"            androID:textcolor="@androID:color/holo_blue_dark"            app:layout_constraineDWIDth="true"            app:layout_constraintBaseline_toBaselineOf="@ID/greenTextVIEw"            app:layout_constraintEnd_toStartOf="@ID/orangetextVIEw"            app:layout_constraintStart_toEndOf="@ID/greenTextVIEw"            tools:text="Another TextVIEw " />        <TextVIEw            androID:ID="@+ID/orangetextVIEw"            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:layout_marginEnd="8dp"            androID:ellipsize="end"            androID:maxlines="1"            androID:textcolor="@androID:color/holo_orange_dark"            app:layout_constraineDWIDth="true"            app:layout_constraintBaseline_toBaselineOf="@ID/blueTextVIEw"            app:layout_constraintEnd_toEndOf="parent"            app:layout_constraintStart_toEndOf="@ID/blueTextVIEw"            tools:text="Another TextVIEw" />    </androID.support.constraint.ConstraintLayout>

这些是使用样本布局的一些情况

没有TextVIEws是椭圆化的:

第一个TextVIEw是椭圆化:

最后一个TextVIEw没有椭圆化并将其他文件推出屏幕:

这些是一些未涵盖所有案例的可能解决方案

使用androID:maxWIDth =“90dp”来阻止TextVIEw增长,但也不必要地限制文本:

使用androID:layout_wIDth =“0dp”启用“match_constraint”也不是最佳解决方案,因为布局将为每个TextVIEw保留三分之一的显示宽度,即使文本小于:

解决方法 使用FlexBox

<com.Google.androID.flexBox.FlexBoxLayout        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="wrap_content"        app:layout_constrainttop_totopOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:alignContent="flex_start"        app:alignItems="flex_start"        app:flexWrap="nowrap"        app:divIDerDrawable="@drawable/ic_divIDer"        app:showdivIDer="mIDdle">    <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:maxlines="1"            androID:textSize="18sp"            app:layout_minWIDth="50dp"            androID:textcolor="@androID:color/holo_green_dark"            androID:ellipsize="end"            androID:text="Text VIEw"/>    <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:maxlines="1"            androID:ellipsize="end"            androID:textcolor="@androID:color/holo_blue_dark"            androID:textSize="18sp"            app:layout_minWIDth="50dp"            androID:text="Another TextVIEw"/>    <TextVIEw            androID:layout_wIDth="wrap_content"            androID:layout_height="wrap_content"            androID:maxlines="1"            androID:textcolor="@androID:color/holo_orange_dark"            androID:textSize="18sp"            app:layout_minWIDth="50dp"            androID:ellipsize="end"            androID:text="Another TextVIEw"/></com.Google.androID.flexBox.FlexBoxLayout>

此布局的输出:

总结

以上是内存溢出为你收集整理的android – 如何使用ConstraintLayout水平对齐三个TextView全部内容,希望文章能够帮你解决android – 如何使用ConstraintLayout水平对齐三个TextView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126234.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存