android - RecyclerView 中的文本溢出屏幕

标签 android android-layout android-recyclerview

RecyclerView Text Truncation

如您所见,我遇到了 RecyclerView 中的文本换行,但其中一些不在屏幕上(突出显示的部分是匹配的部分)

Full text

我将包含 RecyclerView 的 fragment_news 加载到 activity_main 的 FrameLayout 中,news_list_item 定义了 RecyclerView 中的项目。我想我在某处弄乱了一些属性,但我不确定是哪一个。

这是我使用的 xml 代码:

activity_main.xml

<android.support.constraint.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/navigation"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

fragment 新闻.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".news.NewsFragment">

<android.support.v7.widget.RecyclerView
    android:id="@+id/news_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>

</FrameLayout>

新闻列表项目.xml

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/list_item_padding_vertical"
android:paddingLeft="@dimen/list_item_padding_horizontal"
android:paddingRight="@dimen/list_item_padding_horizontal"
android:paddingTop="@dimen/list_item_padding_vertical" >

<ImageView
    android:id="@+id/news_item_image"
    android:layout_width="120dp"
    android:layout_height="90dp"
    app:layout_constraintBottom_toBottomOf="@+id/guideline"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<TextView
    android:id="@+id/news_item_headline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:layout_marginLeft="8dp"
    app:layout_constraintBottom_toBottomOf="@+id/guideline"
    app:layout_constraintLeft_toRightOf="@id/news_item_image"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.5"/>

</android.support.constraint.ConstraintLayout>

最佳答案

已更新(ConstraintLayout 1.1.+)

使用 app:layout_constrainedWidth="true" 宽度设置为 wrap_content 在你的 TextView 中。 同时在 TextView 中将宽度设置为 0dp

以前(已弃用):

app:layout_constraintWidth_default="wrap" 宽度设置为 0dp

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/news_item_image"
        android:layout_width="120dp"
        android:layout_height="90dp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <TextView
        android:id="@+id/chat_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginStart="64dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintWidth_default="wrap"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background="@drawable/chat_message_bubble"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sodales accumsan tortor at bibendum." />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5"/>

</android.support.constraint.ConstraintLayout>

关于android - RecyclerView 中的文本溢出屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815786/

相关文章:

java - 是否可以将 ofFloat 设置为多个目标?

android - Cardview 设置背景颜色

android - 如何根据另一个人的宽度为TextView的第一行设置缩进

android - 如何在不同的 Activity 中接收 CameraActivities 结果(即不在启动 Activity 中)?

android - Android播放两首歌曲的问题

java - 这个 XML 有什么问题吗?滑动抽屉/布局

android - RecyclerView.Adapter notifyItemRangeChanged() 导致重新绑定(bind)错误的 View

android - 使用 FirebaseRecycleradapter 删除项目时 RecyclerView 索引未更新

java - 延迟加载无法正常工作

Android:矩阵-> preconcat 和 postconcat 有什么区别?