java - 防止单行 TextView 椭圆受约束布局中的标签约束

标签 java android android-layout kotlin android-constraintlayout

我有两个 TextView,它们与顶部 TextView 垂直对齐,充当底部 TextView 的标签。我们可以将它们称为 testLabel 和 testText。为了保持它们左对齐,testText 对 testLabel 有 start -> startOf 约束。这些 TextView 位于父级的右上角。 testLabel 对父级有一个 end -> endOf 约束,并带有边距值。问题是标签的长度可能会有所不同,当标签的长度足够小时,标签将根据父级的边距和约束设置正确更新其位置,但这也会导致文本对齐其开头。导致文本超出屏幕,如果文本足够长,则显示省略号。我可以说文本的长度是佩里恒定的。我创建了一个简单的 Activity 来说明问题。 这是标签不够小但一切看起来都正常的情况: enter image description here 当标签足够小时: enter image description here

这是 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/testLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="32dp"
        android:layout_marginRight="32dp"
        android:text="@android:string/ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/testText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="6dp"
        android:layout_marginRight="6dp"
        android:gravity="start"
        android:singleLine="true"
        android:text="MonkeyBars"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/testLabel"
        app:layout_constraintTop_toBottomOf="@id/testLabel" />

</androidx.constraintlayout.widget.ConstraintLayout>

我希望发生的是意识到文本将溢出的约束,并且只满足文本不溢出的边距。我确信我可以通过编程来做一些事情,但希望这是我可以在布局中实现的事情。

最佳答案

testText TextView 中删除此行:

app:layout_constraintStart_toStartOf="@id/testLabel"

并在textLabel TextView中添加此行:

app:layout_constraintStart_toStartOf="@id/testText"

希望它能如你所愿。

关于java - 防止单行 TextView 椭圆受约束布局中的标签约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60564933/

相关文章:

java - 快速 Java 问题 : Instantiating a given class only from another?

java - 可变实例对象的可见性

c# - 将图像传递到 Android ListView ArrayAdapter

java - 我正在尝试将旋转器数据存储到 SQLite 数据库中

android - 如何在线性布局中获取textview的位置(Y坐标)

android - 从自己的自定义 Activity 中关闭 android 中的底部表单对话框

android - 工具栏不从内部折叠工具栏显示

java - 从不同 fragment 获取对象的更改

java - java中如何判断路径是相对的还是绝对的?

android - 尝试动态插入相对布局时获取 ClassCastException?