本文介绍了ellipsize 不适用于 Textview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这 2 个作品

 <文本视图android:id="@+id/item2"android:layout_below="@id/item1"android:layout_width="match_parent"android:layout_height="wrap_content"机器人:椭圆尺寸=结束"机器人:maxLines =3"机器人:paddingLeft =10dp"android:paddingRight="120dp"android:paddingTop="5dp"/>

这不起作用

 

那么,什么是解决方案.为什么会有这种不一致?为什么当我有椭圆形结束和最大线时它起作用,但当我有椭圆形中线和最大线时不起作用.为什么它适用于单线以及椭圆中间而不适用于最大线?谢谢你的帮助.我需要 ellipsize middle 和 maxlines 3.

解决方案

这里是 TextView 文档中的信息

public void setEllipsize (TextUtils.TruncateAt where)
API 中添加一级

导致文本中长度大于视图宽度的单词椭圆形而不是中间断裂.您可能还想setSingleLine() 或 setHorizo​​ntallyScrolling(boolean) 来约束文本到单行.使用 null 关闭省略号.如果setMaxLines(int) 已经用于设置两行或更多行,只有 END 和支持 MARQUEE(其他省略号类型不会做任何事情).

然而,我为多行制作了非常简单的 ellipsize="middle".当然它应该在空闲时间更多地升级,但它在这里.

这是要粘贴到包根目录中的小部件类

import android.content.Context;导入 android.util.AttributeSet;导入 android.widget.TextView;公共类 MiddleMultilineTextView 扩展了 TextView {私人字符串符号=...";私人最终 int SYMBOL_LENGTH = SYMBOL.length();public MiddleMultilineTextView(上下文上下文,AttributeSet attrs){超级(上下文,属性);}@覆盖protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);如果(getMaxLines()> 1){int originalLength = getText().length();int可见长度 = getVisibleLength();如果(原始长度>可见长度){setText(smartTrim(getText().toString(),visibleLength - SYMBOL_LENGTH));}}}私人字符串 smartTrim(字符串字符串,int maxLength){如果(字符串==空)返回空;如果(最大长度<1)返回字符串;if (string.length() 

这是在布局中使用的自定义小部件:

结果:

代码基于:
https://stackoverflow.com/a/8798989/619673
https://stackoverflow.com/a/831583/619673

This 2 works

This does not work

So, what is the soluton. and why such incosistency? Why does it work when I HAVE ellipsize end and maxlines, but not when i have ellipsize middle and max lines. And why it works with singleline as well the ellipsize middle and not on maxlines? Thank You for help. I need ellipsize middle and maxlines 3.

解决方案

Here is information from documentation of TextView


However I've made very simple ellipsize="middle" for multiline. Of course it should be more upgraded in free time but here it is.

This is class of widget to paste into root of package

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

public class MiddleMultilineTextView extends TextView {

    private String SYMBOL = " ... ";
    private final int SYMBOL_LENGTH = SYMBOL.length();

    public MiddleMultilineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

        if (getMaxLines() > 1) {
            int originalLength = getText().length();
            int visibleLength = getVisibleLength();

            if (originalLength > visibleLength) {
                setText(smartTrim(getText().toString(), visibleLength - SYMBOL_LENGTH));
            }
        }
    }

    private String smartTrim(String string, int maxLength) {
        if (string == null)
            return null;
        if (maxLength < 1)
            return string;
        if (string.length() <= maxLength)
            return string;
        if (maxLength == 1)
            return string.substring(0, 1) + "...";

        int midpoint = (int) Math.ceil(string.length() / 2);
        int toremove = string.length() - maxLength;
        int lstrip = (int) Math.ceil(toremove / 2);
        int rstrip = toremove - lstrip;

        String result = string.substring(0, midpoint - lstrip) + SYMBOL + string.substring(midpoint + rstrip);
        return result;
    }

    private int getVisibleLength() {
        int start = getLayout().getLineStart(0);
        int end = getLayout().getLineEnd(getMaxLines() - 1);
        return getText().toString().substring(start, end).length();
    }
}

This is custom widget to use in layout:

<com.example.middlemultiline.MiddleMultilineTextView
        android:id="@+id/item2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="middle"
        android:maxLines="4"
        android:text="Months earlier, in May 2007, a typically busy time for construction work, he sat home for two weeks without any jobs lined up, the first time that had ever happened in all the years he’d been an independent contractor. It was an early indication that hard times were ahead. By fall, he tried to find a steady job with a construction company but by then no one was hiring. And now he no longer had the extra income to support his wife’s entrepreneurial effort — a coffee vending machine business — so that went under too." />

Result:

Code was based on:
https://stackoverflow.com/a/8798989/619673
https://stackoverflow.com/a/831583/619673

这篇关于ellipsize 不适用于 Textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 08:45