android - 如何尊重 RecyclerView 项目内的视图大小
问题描述
我有RecyclerView
一个带有一个图像和两个文本视图的项目。
如果文本太长,则会导致一个项目会比另一个项目大,并且总体上是一个糟糕的 UI。
我可以做些什么来限制文本以尊重另一个视图的宽度 - 在这种情况下的图像?(“三个点”对我来说是正确的解决方案)
我的回收站视图项目的布局代码:
<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="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/cast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/cast_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/cast_image"
app:layout_constraintStart_toStartOf="@+id/cast_image"
app:layout_constraintTop_toBottomOf="@+id/cast_image" />
<TextView
android:id="@+id/cast_char_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/cast_name_text"
app:layout_constraintStart_toStartOf="@+id/cast_name_text"
app:layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
用于澄清问题的图像 -
解决方案
android:layout_width="wrap_content"
好吧,您正在通过在每个视图中使用来强制每个项目采用所需的宽度。如果您可以修复 ImageView 的宽度,那么通过android:maxWidth="XXdp"
在 Text 视图中使用,您可以实现您想要的。
代码如下 -
<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="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/cast_image"
android:layout_width="100dp" //here fixing width to 100dp
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<TextView
android:id="@+id/cast_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="100dp" //fixing TextView to max 100dp
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/cast_image"
app:layout_constraintStart_toStartOf="@+id/cast_image"
app:layout_constraintTop_toBottomOf="@+id/cast_image" />
<TextView
android:id="@+id/cast_char_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="@+id/cast_name_text"
app:layout_constraintStart_toStartOf="@+id/cast_name_text"
app:layout_constraintTop_toBottomOf="@+id/cast_name_text" />
</androidx.constraintlayout.widget.ConstraintLayout>
以下 xml 将导致 -
快乐编码!
推荐阅读
- javascript - 如何使用通过 XML 文件搜索的 JavaScript 执行搜索功能?
- node.js - Next JS 生产路线
- svelte - 如何在 Sapper.js 中使离线数据存储工作?
- ruby-on-rails - Rails 6.0.3 控制器测试错误
- python - 当我的网格不是正方形时,我的索引失败
- javascript - node.js 中的顺序 HTTP 发布请求
- c++ - GCC 10.2 中是否缺少 std::basic_istringstream 的构造函数,如果是,我如何使用自定义分配器构造一个?
- c++builder - 项目迁移后未解决的外部问题
- c# - C# 2DArray IndexOutOfBound,为什么?
- reactjs - setState 不会立即更新