繁体   English   中英

约束布局中心文本位置并结束对其他小部件的引用

[英]Constraint Layout Centre Text Position with End Reference to Other Widget

我有一个带有导航栏的对话框小部件,其中包含标题和关闭按钮。 我想将标题放在设备的中心,并将关闭按钮放在布局的末尾。 但是,当标题很长时,它将与关闭按钮重叠。 这是我的代码:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:maxLines="1"
        android:ellipsize="end"
        android:text="Title" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Close" />

</androidx.constraintlayout.widget.ConstraintLayout>

任何解决方法来实现这一目标?

谢谢你。

通常,要使文本居中,您会将文本的开始和结束限制在居中区域的结束点。 在这种情况下,由于文本应该在屏幕上居中,您可以将开始附加到父开始,将结束附加到父结束。 除非居中的文本超出空的中心区域并与“关闭” TextView重叠,否则此方法有效。 不幸的是,没有办法做出这些约束并告诉居中视图避免与右侧的TextView重叠。

为了使其工作,我将引入另一个TextView ,它是“关闭” TextView的副本,使其不可见并附加到父项的顶部和开始处。 这将在两个TextViews之间创建一个区域,文本可以在其中居中。 为确保文本不与结束TextView重叠,请在居中视图上指定app:layout_constrainedWidth="true" 像这样的东西:

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/space"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        android:visibility="invisible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="This is some very long text that should stretch across the device and ellipsize."
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintStart_toEndOf="@+id/space"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Close"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Barrier
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="left"
        tools:layout_editor_absoluteX="306dp"
        tools:layout_editor_absoluteY="62dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

对于长文本,这将在设计器中显示如下:

在此处输入图片说明

并喜欢以下短文本:

在此处输入图片说明

还有其他方法可以做到这一点,这将涉及一些编码,但这是一种纯 XML 解决方案,IMO 更可取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM