这 2 个作品
Run Code Online (Sandbox Code Playgroud)<TextView android:id="@+id/item2" android:layout_below="@id/item1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="middle" android:singleLine="true" android:paddingLeft="10dp" android:paddingRight="120dp" android:paddingTop="5dp" /> <TextView android:id="@+id/item2" android:layout_below="@id/item1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="3" android:paddingLeft="10dp" android:paddingRight="120dp" android:paddingTop="5dp" />
这不起作用
Run Code Online (Sandbox Code Playgroud)<TextView android:id="@+id/item2" android:layout_below="@id/item1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="middle" android:maxLines="3" android:paddingLeft="10dp" android:paddingRight="120dp" android:paddingTop="5dp" />
那么,什么是解决方案。为什么会有这种不一致?为什么当我有椭圆形结束和最大线时它起作用,但当我有椭圆形中线和最大线时不起作用。为什么它适用于单线以及椭圆中间而不适用于最大线?谢谢你的帮助。我需要 ellipsize middle 和 maxlines 3。
这是来自 TextView 文档的信息
public void setEllipsize (TextUtils.TruncateAt where)
在 API 级别 1 中添加导致文本中长于视图宽度的单词被椭圆化而不是在中间断开。您可能还需要 setSingleLine() 或 setHorizontallyScrolling(boolean) 将文本限制为单行。使用 null 关闭省略号。如果 setMaxLines(int) 已经用于设置两行或更多行,则仅支持 END 和 MARQUEE(其他省略号类型不会做任何事情)。
但是,我为多行制作了非常简单的 ellipsize="middle"。当然,它应该在空闲时间进行更多升级,但它就在这里。
这是要粘贴到包根目录中的小部件类
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是在布局中使用的自定义小部件:
<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." />
Run Code Online (Sandbox Code Playgroud)
结果:
代码基于:
https
:
//stackoverflow.com/a/8798989/619673
/sf/answers/58210841/
| 归档时间: |
|
| 查看次数: |
6552 次 |
| 最近记录: |