Android開發(fā)自定義TextView省略號(hào)樣式的方法
本文實(shí)例講述了Android開發(fā)自定義TextView省略號(hào)樣式的方法。分享給大家供大家參考,具體如下:
在布局xml中設(shè)置textView的字段 android:maxLines="2" android:ellipsize="end"字段之后,textview會(huì)自動(dòng)壓縮行數(shù),并且對(duì)壓縮掉的部分用...顯示。如果不想用...而想用。。?;蛘?.. ...就需要自定義這個(gè)省略號(hào)的樣式,不需要自定義控件,方法如下。
首先是布局文件
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:maxLines="2"
android:ellipsize="end"
android:text="This is a text of TextView This is a text of TextView This is a text of TextView This is a text of TextView This is a text of TextView"
android:textSize="20sp" />
對(duì)textView進(jìn)行代碼控制
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView4);
Layout layout = textView.getLayout();//此時(shí)的layout為null,必須用觀察者模式的回調(diào)函數(shù)來(lái)獲取layout
System.out.println("layout是"+layout);//此處為null
ViewTreeObserver observer = textView.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
boolean isfirstRunning = true;
@Override
public void onGlobalLayout() {//此方法會(huì)被調(diào)用兩次,每執(zhí)行一次settext就會(huì)執(zhí)行一次,第一次是顯示全部的文本,不加省略,第二次是加上省略,將會(huì)刪減兩次文本
// TODO Auto-generated method stub
if(isfirstRunning==false)return;//如果不加這一行的條件,出來(lái)之后是完整單詞+。。。,如果加上,出來(lái)就是截?cái)嗟膯卧~+。。。
Layout layout2 = textView.getLayout();
System.out.println("layout2是"+layout2);
if(textView!=null&&layout2!=null){
int lines = layout2.getLineCount();
System.out.println("當(dāng)前行數(shù)是"+layout2.getLineCount());
System.out.println("被省略的字符數(shù)量是"+layout2.getEllipsisCount(lines-1));//看看最后一行被省略掉了多少
System.out.println("被省略的字符起始位置是"+layout2.getEllipsisStart(lines-1));//看看最后一行被省略的起始位置
System.out.println("最后一個(gè)可見字符的偏移是"+layout2.getLineVisibleEnd(lines-1));
//開始替換系統(tǒng)省略號(hào)
if(lines<2)return;//如果只有一行,就不管了
if(layout2.getEllipsisCount(lines-1)==0)return;//如果被省略的字符數(shù)量為0,就不管了
String showText = textView.getText().toString();
System.out.println("刪減前"+showText);
showText = showText.substring(0, layout2.getLineVisibleEnd(lines-1)-6).concat("。。。");//在此處自定義要顯示的字符
System.out.println("刪減后"+showText);
textView.setText(showText);
isfirstRunning=false;
}
}
});
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中TextView實(shí)現(xiàn)超過(guò)固定行數(shù)顯示“...展開全部”
- Android中TextView實(shí)現(xiàn)分段顯示不同顏色的字符串
- Android實(shí)現(xiàn)可點(diǎn)擊展開的TextView
- Android自定義可點(diǎn)擊的ImageSpan并在TextView中內(nèi)置View
- android使用Textview實(shí)現(xiàn)伸縮效果
- Android實(shí)現(xiàn)可以展開的TextView
- Android TextView實(shí)現(xiàn)詞組高亮的示例代碼
- Android中實(shí)現(xiàn)詞組高亮TextView方法示例
- Android Textview實(shí)現(xiàn)顏色漸變滾動(dòng)效果
- Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)
- Android中TextView局部變色功能實(shí)現(xiàn)
相關(guān)文章
Android Activity跳轉(zhuǎn)動(dòng)畫效果
這篇文章主要介紹了Android Activity跳轉(zhuǎn)動(dòng)畫效果,本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友可以參考下2017-02-02
ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果
這篇文章主要為大家詳細(xì)介紹了ExpandableListView實(shí)現(xiàn)手風(fēng)琴效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
詳解Android使用Html.fromHtml需要注意的地方
本篇文章主要介紹了詳解Android使用Html.fromHtml需要注意的地方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
完美解決Android三星手機(jī)從圖庫(kù)選擇照片旋轉(zhuǎn)問(wèn)題
這篇文章主要幫助大家完美解決了Android三星手機(jī)從圖庫(kù)選擇照片旋轉(zhuǎn)問(wèn)題,很實(shí)用的解決小案例,感興趣的小伙伴們可以參考一下2016-04-04
Android實(shí)現(xiàn)自定義圓形進(jìn)度條
這篇文章主要介紹了Android自定義圓形進(jìn)度條實(shí)現(xiàn)代碼,進(jìn)度條在Android中教程經(jīng)常使用到,本文向大家分享了Android實(shí)現(xiàn)自定義圓形進(jìn)度條的代碼,感興趣的小伙伴們可以參考一下2016-03-03
Android RecyclerView實(shí)現(xiàn)點(diǎn)擊條目刪除
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView實(shí)現(xiàn)點(diǎn)擊條目刪除,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Kotlin之自定義 Live Templates詳解(模板代碼)
這篇文章主要介紹了Kotlin之自定義 Live Templates詳解(模板代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Flutter網(wǎng)絡(luò)請(qǐng)求庫(kù)DIO的基本使用
這篇文章主要介紹了Flutter網(wǎng)絡(luò)請(qǐng)求庫(kù)DIO的基本使用,幫助大家更好的理解和學(xué)習(xí)使用Flutter,感興趣的朋友可以了解下2021-04-04
Android上實(shí)現(xiàn)RTSP服務(wù)器的方法
在Android平臺(tái)實(shí)現(xiàn)RTSP服務(wù)器是一項(xiàng)挑戰(zhàn)性任務(wù),旨在無(wú)需部署獨(dú)立的RTSP/RTMP服務(wù),通過(guò)內(nèi)置輕量級(jí)RTSP服務(wù),實(shí)現(xiàn)本地音視頻數(shù)據(jù)的對(duì)外共享,本文介紹Android上實(shí)現(xiàn)RTSP服務(wù)器的方法,感興趣的朋友一起看看吧2024-11-11

