ConstraintLayout子文本视图剪切文本

时间:2018-01-13 08:39:30

标签: android textview android-constraintlayout

我使用RecyclerView布置ConstraintLayout的单元格,并将元素TextView (id:cell_summary)切断其长文本;

layout screenshot

这里是单元格布局xml代码:

<?xml version="1.0" encoding="utf-8"?>    
<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="wrap_content">

  <TextView
    android:id="@+id/cell_title"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:maxLines="2"
    android:text="And we find ourselves cruelly cut off from the wireless world"
    android:textColor="@android:color/black"
    android:textSize="16sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

  <ImageView
    android:id="@+id/cell_thumb"
    android:layout_width="120dp"
    android:layout_height="88dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="8dp"
    android:background="#cccccc"
    android:scaleType="centerCrop"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@id/cell_title" />

  <TextView
    android:id="@+id/cell_datetime"
    android:layout_width="0dp"
    android:layout_height="15dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="16dp"
    android:text="2Hours"
    android:textColor="#999999"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@id/cell_thumb"
    app:layout_constraintLeft_toRightOf="@id/cell_thumb"
    app:layout_constraintRight_toRightOf="parent" />

  <TextView
    android:id="@+id/cell_summary"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="12dp"
    android:text="But then we were not being singled out, the entire country lives in a bubble of unreality, cut off from the outside world and watched by an army of informers."
    android:ellipsize="end"
    app:layout_constraintBottom_toTopOf="@id/cell_datetime"
    app:layout_constraintLeft_toLeftOf="@id/cell_datetime"
    app:layout_constraintRight_toRightOf="@id/cell_datetime"
    app:layout_constraintTop_toTopOf="@id/cell_thumb" />

  <View
    android:layout_width="0dp"
    android:layout_height="0.5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="16dp"
    android:background="#dddddd"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/cell_thumb" />

</android.support.constraint.ConstraintLayout>

如果我添加android:maxLines="3"TextView将正常运行。但在这里,我需要一个无界TextView

2 个答案:

答案 0 :(得分:0)

尝试以下代码,这将有助于您: -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:orientation="vertical">

<TextView
    android:id="@+id/cell_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="2"
    android:text="And we find ourselves cruelly cut off from the wireless world"
    android:textColor="@android:color/black"
    android:textSize="16sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/cell_thumb"
        android:layout_width="120dp"
        android:layout_height="88dp"
        android:layout_gravity="center_vertical"
        android:background="#cccccc"
        android:scaleType="centerCrop" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/cell_summary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:text="But then we were not being singled out, the entire country lives in a bubble of unreality, cut off from the outside world and watched by an army of informers." />

        <TextView
            android:id="@+id/cell_datetime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2Hours"
            android:textColor="#999999"
            android:textSize="12sp" />
    </LinearLayout>
</LinearLayout>


<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="#dddddd" />

答案 1 :(得分:0)

从cell_summary

中删除android:layout_marginBottom="12dp"