日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android TextView 实现一个单词分两行显示

發(fā)布時(shí)間:2023/12/29 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android TextView 实现一个单词分两行显示 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

今天遇到一個(gè)需求,TextView實(shí)現(xiàn)自動(dòng)換行時(shí)一個(gè)英文單詞能夠換行顯示,使布局整齊。通過網(wǎng)上查詢,確定實(shí)現(xiàn)邏輯如下:

自定義TextView,重寫其onMeasure方法,在測(cè)量textView的寬高時(shí)重新拆分字符串,實(shí)現(xiàn)單個(gè)單詞換行顯示,以下是自定義TextView的源碼(基本復(fù)制博客:http://www.cnblogs.com/snser/p/5159125.html中的源碼):

package com.example.lupeng.customtextviewbreak;import android.content.Context; import android.graphics.Paint; import android.text.SpannableString; import android.text.Spanned; import android.text.TextUtils; import android.util.AttributeSet; import android.widget.TextView;/*** 單個(gè)單詞可以顯示在兩行的TextView* 缺點(diǎn):setTextView時(shí)如果設(shè)置了SpannableString,則還需要調(diào)用本TextView的setSpan方法再次設(shè)置,而且由于Text換行導(dǎo)致設(shè)置Spannable時(shí)可能出現(xiàn)異常* Created by lupeng.kang on 17/1/10.*/ public class BreakTextView extends TextView {private boolean mEnabled = true;public BreakTextView(Context context) {super(context);}public BreakTextView(Context context, AttributeSet attrs) {super(context, attrs);}public BreakTextView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}/*** 設(shè)置單行展示不下一個(gè)單詞時(shí)是否自動(dòng)截?cái)?* @param enable*/public void setAutoSplit(boolean enable) {mEnabled = enable;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {if (getWidth() > 0 && getHeight() > 0 && mEnabled) {CharSequence newText = autoSplitText(this);if (!TextUtils.isEmpty(newText)) {setText(newText);}}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}/*** 自動(dòng)折斷單詞* 實(shí)現(xiàn)思路:測(cè)量TextView寬度時(shí),測(cè)量每行Text的寬度,如果一行展示不下某個(gè)單詞,就將這個(gè)單詞折斷** @param tv* @return*/private CharSequence autoSplitText(final TextView tv) {final CharSequence rawCharSequence = tv.getText();final String rawText = rawCharSequence.toString(); //原始文本final Paint tvPaint = tv.getPaint(); //paint,包含字體等信息final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用寬度//將原始文本按行拆分String[] rawTextLines = rawText.replaceAll("\r", "").split("\n");StringBuilder sbNewText = new StringBuilder();for (String rawTextLine : rawTextLines) {if (tvPaint.measureText(rawTextLine) <= tvWidth) {//如果整行寬度在控件可用寬度之內(nèi),就不處理了sbNewText.append(rawTextLine);} else {//如果整行寬度超過控件可用寬度,則按字符測(cè)量,在超過可用寬度的前一個(gè)字符處手動(dòng)換行float lineWidth = 0;for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {char ch = rawTextLine.charAt(cnt);lineWidth += tvPaint.measureText(String.valueOf(ch));if (lineWidth <= tvWidth) {sbNewText.append(ch);} else {if (cnt - 2 >= 0 && rawTextLine.charAt(cnt - 1) >= 'A' && rawTextLine.charAt(cnt - 1) <= 'z' && rawTextLine.charAt(cnt - 2) >= 'A' && rawTextLine.charAt(cnt - 2) <= 'z') {sbNewText.deleteCharAt(sbNewText.length() - 1);sbNewText.append("-\n");lineWidth = 0;cnt -= 2;} else {sbNewText.append("\n");lineWidth = 0;--cnt;}}}}sbNewText.append("\n");}//把結(jié)尾多余的\n去掉if (!rawText.endsWith("\n")) {sbNewText.deleteCharAt(sbNewText.length() - 1);}//使用TextView設(shè)置的Span格式SpannableString sp = new SpannableString(sbNewText.toString());if (rawCharSequence instanceof Spanned) {TextUtils.copySpansFrom((Spanned) rawCharSequence, 0, rawCharSequence.length(), null, sp, 0);}return sp;}} 接下來我們就可以像使用TextView一樣使用BreakTextView了。

以上代碼是復(fù)制的http://www.cnblogs.com/snser/p/5159125.html中的代碼,并做了優(yōu)化:

1、英文單詞換行中間要加連接符 “-”,

2、autoSlitText方法返回CharSequence類型,使設(shè)定的span格式有效。

總結(jié)

以上是生活随笔為你收集整理的Android TextView 实现一个单词分两行显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。