TextViewのellipsizeサフィックスをカスタマイズします



Customize Ellipsize Suffix Textview



左:通常の楕円サイズ効果
右:接尾辞「>>」の付いた楕円サイズ効果
画像

<com.gdeer.gdtesthub.textView.EllipsizeTextView android:id='@+id/tv_special' android:layout_width='58dp' android:layout_height='wrap_content' android:text='TextFragment' android:textColor='#ffffff' android:maxLines='2' android:ellipsize='end'/> public class EllipsizeTextView extends AppCompatTextView { // from android.text.TextUtils private static final String ELLIPSIS_NORMAL = 'u2026' // (…) public static final String ELLIPSIS_FILLER = 'uFEFF' // blank characters String suffix = '>>' public EllipsizeTextView(Context context) { super(context) } public EllipsizeTextView(Context context, AttributeSet attrs) { super(context, attrs) } public EllipsizeTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr) } @Override public void setText(CharSequence text, BufferType type) { super.setText(text, type) Layout layout = getLayout() if (layout != null) { // the original text entered String rawText = String.valueOf(layout.getText()) Log.d('zhangjl', 'rawText:' + rawText + ' length:' + rawText.length()) if (rawText.contains(ELLIPSIS_NORMAL)) { // Remove whitespace characters, the ellipsize effect that comes with TextView is to convert characters beyond the length into whitespace characters. String trimText = trimBlankChar(rawText) Log.d('zhangjl', 'trimText:' + trimText + ' length:' + trimText.length()) String targetText = trimText String resultText = '' // number of characters removed int reduceCount = 1 while (!resultText.equals(targetText)) { targetText = rawText.subSequence(0, trimText.length() - reduceCount) + ELLIPSIS_NORMAL + suffix Log.d('zhangjl', 'targetText:' + targetText + ' length:' + targetText.length() + ' reduceCount:' + reduceCount) super.setText(targetText, type) layout = getLayout() resultText = trimBlankChar(String.valueOf(layout.getText())) Log.d('zhangjl', 'resultText:' + resultText + ' length:' + resultText.length() + ' reduceCount:' + reduceCount) reduceCount++ } } } } public String trimBlankChar(String rawText) { int index = rawText.indexOf(ELLIPSIS_FILLER) String trimText = rawText if (index > 0) { trimText = rawText.substring(0, index) } return trimText } }