我遇到了TextView的一些奇怪行为,包括重量和多行文字:
如您所见,文本的最后一行被截断。我想要的是椭圆化上一行末尾的文本(quis ...)。我的代码在这里:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/events_item_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="@string/events"
android:padding="@dimen/events_item_padding"
android:src="@drawable/donations_image" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:id="@+id/events_item_title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\nAt vero eos et accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium voluptatum deleniti atque corrupti, quos dolores et quas molestias excepturi sint, obcaecati cupiditate non provident, similique sunt in culpa, qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio, cumque nihil impedit, quo minus id, quod maxime placeat, facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet, ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat" />
<TextView
android:id="@+id/events_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:text="Some text on the bottom" />
</LinearLayout>
所以,我已经结束了这个定制视图:
public class ShrinkingTextView extends TextView {
private int measuresLeft = 3;
public ShrinkingTextView(Context context) {
super(context);
}
public ShrinkingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measuresLeft--;
if (measuresLeft <= 0) {
if (getLineCount() * getLineHeight() > getMeasuredHeight()) {
setText(getText().subSequence(0, getText().length() - 4) + "...");
measure(widthMeasureSpec, heightMeasureSpec);
}
}
}
}
答案 0 :(得分:1)
这是一种方法:
private void fixTextCutting(final TextView text) {
ViewTreeObserver vto = text.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
private int maxLines = -1;
@Override
public void onGlobalLayout() {
if (maxLines < 0 && text.getHeight() > 0 && text.getLineHeight() > 0) {
int height = text.getHeight();
int lineHeight = text.getLineHeight();
maxLines = height / lineHeight;
text.setMaxLines(maxLines);
}
}
});
}
答案 1 :(得分:0)
您没有为TextView设置最大行数,请改为尝试:
使用此
<TextView
android:id="@+id/events_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="5"
android:text="@string/your_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
并根据需要以编程方式设置行数。
答案 2 :(得分:0)
您可以在任何类型的ViewGroup中为TextView使用全局布局侦听器。我指出了removeOnGlobalLayoutListener的重要性,特别是如果你在Adapter for RecyclerView或ListView中使用监听器。
final TextView dSTextView = (TextView)findViewById(R.id.annoyingTextView);
dSTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
dSTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
float lineHeight = dSTextView.getLineHeight();
int maxLines = (int) (dSTextView.getHeight() / lineHeight);
if (dSTextView.getLineCount() != maxLines) {
dSTextView.setLines(maxLines);
}
}
});
您可以阅读更多相关信息here