这是我的代码和图片 我有轮播recyclerview的地方,每个项目都有很长的文字 如何剪切文本并使文本与imageView的结尾对齐?
<androidx.constraintlayout.widget.ConstraintLayout xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp">
<ImageView
android:id="@+id/asset_image"
android:layout_width="120dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
app:imageUrl="@{imageUrl}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_conversion_wrapHeight="0"
tools:layout_conversion_wrapWidth="0" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="sfsdfsdfsjkfljljljljlklkjlkjkhksdjfskdjflsdjflsdjkflsjkdf"
app:layout_constraintEnd_toEndOf="@+id/asset_image"
app:layout_constraintStart_toStartOf="@+id/asset_image"
app:layout_constraintTop_toBottomOf="@+id/asset_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 0 :(得分:1)
1。设置文本视图宽度以匹配约束(0dp)
2。将TextView的右/ letf约束设置为ImageView的右/左
<?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">
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
tools:src="@tools:sample/avatars"
android:layout_height="250dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/texts"
android:layout_width="0dp"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem/random"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 1 :(得分:-1)
您必须给您的textview width = "match_constraint"
基本上等于0dp
,这样您的textview
width
将等于您在结尾处设置的约束textview
:
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="sfsdfsdfsjkfljljljljlklkjlkjkhksdjfskdjflsdjflsdjkflsjkdf"
app:layout_constraintEnd_toEndOf="@+id/asset_image"
app:layout_constraintStart_toStartOf="@+id/asset_image"
app:layout_constraintTop_toBottomOf="@+id/asset_image" />
答案 2 :(得分:-2)
只需将android:layout_width="wrap_content"
更改为android:layout_width="120dp"