有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java阻止约束布局中受标签约束的单行TextView省略号

我有两个文本视图,它们与作为底部文本视图标签的顶部文本视图垂直对齐。我们可以称之为testLabel和testText。为了使它们保持左对齐,testText有start->;开始测试标签。这些文本视图位于父视图的右上角。testLabel有一个端点->;使用边距值将约束赋给父级。问题在于,标签的长度可能会有所不同,当标签的长度值足够小时,标签将根据父标签的边距和约束设置正确更新其位置,但这也会导致文本对齐其起点。这会导致文本离开屏幕,如果文本足够长,就会显示一个省略号。我可以说文本的长度是非常恒定的。我创建了一个简单的活动来说明这个问题。 当标签不够小,而且看起来一切正常时,情况如下: enter image description here 当标签足够小时: enter image description here

以下是xml:

<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context=".MainActivity">

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

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

</安卓x.constraintlayout.widget.ConstraintLayout>

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


共 (2) 个答案

  1. # 1 楼答案

    如果你想让两个文本视图都左对齐,并且标签的长度可以不同,那么试试这个

    <?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:gravity="start"
            android:singleLine="true"
            android:text="MonkeyBars"
            app:layout_constraintEnd_toEndOf="@id/testLabel"
            app:layout_constraintTop_toBottomOf="@id/testLabel" />
    
  2. # 2 楼答案

    从测试文本中删除这一行:

    app:layout_constraintStart_toStartOf="@id/testLabel"
    

    并在textLabelTextView中添加这一行:

    app:layout_constraintStart_toStartOf="@id/testText"
    

    希望它能如你所愿