maxLength を使用して TextView を 3 つのドットで終了する方法 質問する

maxLength を使用して TextView を 3 つのドットで終了する方法 質問する

TextViewレイアウトに がwrap_contentありますlayout_width。最大 15 文字に制限されているため、 を使用していますmaxLength

これを 3 つのドット (...) で終了する必要がありますTextViewが、これは dp で固定サイズを指定した場合のみ発生しますがlayout_width、これは望ましくないことです。

15 番目の文字以降の文字列をトリミングして 3 つのドットを追加することでプログラム的に可能であることはわかっていますが、私はそれを XML で行うことを好みます。

テキストを 3 つのドットで終了し、wrap_content のままにする方法はありますか?

<TextView
    android:id="@+id/inbox_contactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:lines="1"
    android:maxLines="1"
    android:ellipsize="end"
    android:singleLine="true"
    android:maxLength="15"
    android:textColor="#0670b4"
    android:textSize="16sp" />

ベストアンサー1

これで問題は解決します。XMLellipsizeコードでプロパティを使用してください。

android:ellipsize="end" <!-- This makes the magic ... thing -->
android:maxEms="15" <!-- Limit of the Text -->
android:singleLine="true" <!-- In case if you want everything in one line -->

編集: singleLine非推奨代わりにを使用してくださいmaxlines="1"

おすすめ記事