【问题标题】:TextView does not ellipsizeTextView 没有椭圆
【发布时间】:2021-01-19 08:44:02
【问题描述】:

我检查了这个answer 和这个answer,但仍然无法弄清楚为什么我的椭圆尺寸仍然不起作用。

我在RecyclerView 中使用以下CardView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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/cardview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginVertical="2dp"
    app:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="8dp">

        <android.support.v7.widget.CardView
            android:id="@+id/food_image_card"
            android:layout_width="80dp"
            android:layout_height="80dp">

            <ImageView
                android:id="@+id/food_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/food_image_place_holder" />

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

        <LinearLayout
            android:id="@+id/food_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/food_name_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toEndOf="@id/food_image_card"
                app:layout_constraintTop_toTopOf="@+id/food_image_card">

                <TextView
                    android:id="@+id/food_name"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:text="Food Name"
                    android:textSize="18sp"
                    android:textStyle="bold" />

                <ImageView
                    android:id="@+id/liked_image"
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_marginHorizontal="4dp"
                    android:padding="4dp"
                    app:srcCompat="@drawable/ic_undo_like" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tag_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginVertical="2dp">
                <TextView
                    android:id="@+id/tag_header"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/tag_header" />

                <TextView
                    android:id="@+id/tag_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLines="1"
                    android:ellipsize="end"
                    android:text="Placeholder" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/note_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginVertical="2dp">

                <TextView
                    android:id="@+id/note_header"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/note_header" />

                <TextView
                    android:id="@+id/note_content"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:maxLines="3"
                    android:text="Placeholder" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

虽然tag_contentnote_content 在Android Studio 中看起来是椭圆的(第一张图片),但在我的手机上它们实际上并没有椭圆(第二张图片的第一条)。任何想法为什么?

我认为xml没有问题。它没有椭圆的原因是我更改了适配器中的文本。

这是我的项目website,发布的代码是here,将在这个adapter中使用

【问题讨论】:

    标签: android


    【解决方案1】:

    你已经强制 TextView note_content 有 3 行,所以如果内容可以放在 3 行中,它就不会被省略。

     <TextView
              android:id="@+id/note_content"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ellipsize="start"
              android:maxLines="3"
              android:text="Placeholder" />
    

    如果您更改android:maxLines="1",文本将被省略。

    【讨论】:

    • 对不起,note_content 实际上有超过 3 行。 tag_content 也一样。它的内容不止一行,但没有省略。
    • 请忽略第二张图片上的第二项。我实现了...... maunally。
    • hmm 可以是语言吗,你能不能试着在单词之间加空格,你能不能试着把android:maxWidth = "20sp 放在tag_content ...
    • 它不起作用。我的手机还是直接断了短信。
    猜你喜欢
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多