Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android TextView限定顯示字數並以省略號結尾的方法

編輯:關於 Android 程式設計

本文將分享Android設置TextView只顯示指定個數字符、超出部分用省略號(...)表示的方法,供有需要的朋友參考。

一、問題描述

今天在公司遇到一個需求:TextView最多顯示8個字符,超出部分顯示省略號。

二、解決方法

網上查了不少資料,有人建議分別設置android:singleLine="true"並配合android:ellipsize="end",但筆者測試後發現並不成功。最終自己摸索出一種可行的方式,供大家參考:

 <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxEms="18" android:singleLine="true" android:ellipsize="end" /> 

關鍵在於:通過android:maxEms與android:singleLine屬性相結合來實現該功能。

更多Android相關內容感興趣的讀者,可查看本站專題:《Android開發入門與進階教程》、《Android Service組件使用技巧總結》、《Android基本組件用法總結》及《Android控件用法總結》。

希望本文所述對大家的Android程序設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved