ConstraintLayout - Ellipsize 开始不起作用
ConstraintLayout - Ellipsize start not working
在 ConstraintLayout 中使用 ellipsize start 会使文本在末尾突然被截断。
我提到我在开始和结束时都限制了视图。
当我切换到椭圆形端时,它工作正常。
我还注意到,在 Android Studio 预览屏幕中,文本放置正确,但在运行时,它在末尾被截断。
他有关于此的已知问题吗?还是我做错了什么?
这是我在 XML 中使用的代码:
<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="wrap_content">
<ImageView
android:id="@+id/location"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="@dimen/small_margin"
android:alpha="0.38"
android:contentDescription="@string/location"
android:src="@drawable/ic_location"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/material_on_surface_disabled" />
<TextView
android:id="@+id/area"
style="?attr/textAppearanceCaption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/small_margin"
android:layout_marginBottom="@dimen/small_margin"
android:ellipsize="start"
android:singleLine="true"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/location"
tools:ignore="RtlSymmetry"
tools:text="Long string, very long string that should be longer than the screen size, this is very long" />
</androidx.constraintlayout.widget.ConstraintLayout>
要将 Ellipsize 放在最后,请尝试
android:ellipsize="end"
android:maxLines="1"
要在开始时使用 Ellipsize,请尝试
android:ellipsize="start"
android:singleLine="true"
在这里回答我自己的问题
对于面临相同问题的任何人:我发现这里出了什么问题。
我发现问题不是 ConstraintLayout,而是字体样式。显然,某些字体样式会导致 android:ellipsize="start"
(甚至中间)出现 bug
您可以简单地在 TextView
上使用 android:layoutWidth="0dp"
和 android:singleLine="true"
(我知道它已被弃用,但似乎 maxLines 可能会导致崩溃)并删除字体样式,或者找到一个有效。
我很想知道为什么会这样,如果有人有想法的话
在 ConstraintLayout 中使用 ellipsize start 会使文本在末尾突然被截断。 我提到我在开始和结束时都限制了视图。
当我切换到椭圆形端时,它工作正常。
我还注意到,在 Android Studio 预览屏幕中,文本放置正确,但在运行时,它在末尾被截断。
他有关于此的已知问题吗?还是我做错了什么?
这是我在 XML 中使用的代码:
<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="wrap_content">
<ImageView
android:id="@+id/location"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="@dimen/small_margin"
android:alpha="0.38"
android:contentDescription="@string/location"
android:src="@drawable/ic_location"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/material_on_surface_disabled" />
<TextView
android:id="@+id/area"
style="?attr/textAppearanceCaption"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/small_margin"
android:layout_marginBottom="@dimen/small_margin"
android:ellipsize="start"
android:singleLine="true"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/location"
tools:ignore="RtlSymmetry"
tools:text="Long string, very long string that should be longer than the screen size, this is very long" />
</androidx.constraintlayout.widget.ConstraintLayout>
要将 Ellipsize 放在最后,请尝试
android:ellipsize="end"
android:maxLines="1"
要在开始时使用 Ellipsize,请尝试
android:ellipsize="start"
android:singleLine="true"
在这里回答我自己的问题
对于面临相同问题的任何人:我发现这里出了什么问题。
我发现问题不是 ConstraintLayout,而是字体样式。显然,某些字体样式会导致 android:ellipsize="start"
(甚至中间)出现 bug
您可以简单地在 TextView
上使用 android:layoutWidth="0dp"
和 android:singleLine="true"
(我知道它已被弃用,但似乎 maxLines 可能会导致崩溃)并删除字体样式,或者找到一个有效。
我很想知道为什么会这样,如果有人有想法的话