android override textView根据getMaxLines设置文本

标签 android textview overriding

我有一个扩展 TextView 的 customTextView。

我想覆盖 setText,这样如果某些文本长度大于最大行数,那么我必须显示一些其他文本而不是来自 getText 的文本。

这是我的代码

public class CustomTextView extends TextView {

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);



        setLinksClickable(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        String text = String.valueOf(getText());

        Spanned htmlFormatedText = OmniTextUtil.getHyperLinked(text);

        setText(htmlFormatedText);

        List<CharSequence> charSequenceList = getLines(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            if(getLineCount() > getMaxLines())
            {
                for(int i =0; i < getMaxLines(); i++)
                {
                    text = text + charSequenceList.get(i);
                }

                text = text.substring(0, text.length() -3) + "...";

                setText(text);
            }
        }
    }

    public static List<CharSequence> getLines(TextView view) {
        final List<CharSequence> lines = new ArrayList<>();
        final Layout layout = view.getLayout();

        if (layout != null) {
            // Get the number of lines currently in the layout
            final int lineCount = layout.getLineCount();

            // Get the text from the layout.
            final CharSequence text = layout.getText();

            // Initialize a start index of 0, and iterate for all lines
            for (int i = 0, startIndex = 0; i < lineCount; i++) {
                // Get the end index of the current line (use getLineVisibleEnd()
                // instead if you don't want to include whitespace)
                final int endIndex = layout.getLineEnd(i);

                // Add the subSequence between the last start index
                // and the end index for the current line.
                lines.add(text.subSequence(startIndex, endIndex));

                // Update the start index, since the indices are relative
                // to the full text.
                startIndex = endIndex;
            }
        }
        return lines;
    }

}

我可以设置我的文本,但再次滚动时它会变成普通文本...

最佳答案

如果你只想在文本末尾添加三虚线,那么你可以简单地做到这一点。试试下面的代码

//程序化

        text_view.setEllipsize(TextUtils.TruncateAt.END);
        text_view.setMaxLines(2);//set number of line what ever you want.
        text_view.setText("your long text");

//使用xml

 <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="2"
            android:maxLines="2"
            android:text="your long text"
            android:textColor="@android:color/black" />

如果你想检查文本是否太长试试这个 link .希望对您有所帮助。

关于android override textView根据getMaxLines设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33912183/

相关文章:

overriding - 无法覆盖 prestashop 模块中的类文件

Java方法重写equals方法

android - 更改字体/文本大小时的 Textview 对齐问题

android - 包含 path_provider 时出错

android - Tesseract 解析图像并返回 null

ios - 更改属性 TextView 中一个单词的颜色(objective-c)

java - 如何使 TextView 中的链接不可单击,除了所选链接

android - 为什么 textView "Hello world!"没有显示在屏幕上?

swift - 重写 Swift 文件中的 Objective-C 方法

android - Assets - "images"、 "sounds"和 "webkit"?