【问题标题】:TextView last line drawn in halfTextView 最后一行画了一半
【发布时间】:2012-12-21 19:42:22
【问题描述】:

我有一个高度是动态的 TextView。我想设置如果不能完全绘制最后一行,则不可见。 screenshot

height之所以是动态的,是因为它用在一个AppWidget中,而一个appwidget的大小在不同的设备上是不一样的。因此,您无法测量 TextView 或无法使用 TextView 的子类。 link

这是我现在基本使用的。当我将长文本放入 TextView 时,最后一行看起来很丑。

<LinearLayout
android:id="@+id/widgetContent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
    android:id="@+id/sg"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

【问题讨论】:

  • 发布您的 textview 代码一次..这样我们就可以做点什么了。
  • 如果您不在 AppWidget 中,那就太好了。有一个示例应用程序Wiktinary。它在那里使用 android:fadingEdge="vertical" 。它不是最好的,但我认为我会使用它。

标签: android textview


【解决方案1】:

我最近遇到了这个问题并使用了另一种解决方案。

我正在动态设置 maxLines:

final TextView tv = ((TextView) myLayout.findViewById(R.id.mytextview));
tv.setText(value);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    private int maxLines = -1;

    @Override
    public void onGlobalLayout() {
        if (maxLines < 0 && tv.getHeight() > 0 && tv.getLineHeight() > 0) {
            int height = tv.getHeight();
            int lineHeight = tv.getLineHeight();
            maxLines = height / lineHeight;
            tv.setMaxLines(maxLines);
        }
    }
});

【讨论】:

    【解决方案2】:

    试试这个

    Paint  paint = new Paint();
    // set paint with all current text properties of the textview. like text size, face etc,
    
    textPixelLenght = paint.measureText(mText);
    int textviewWidth = mTextView.getMeasuredWidth();
    int numberOfLines = textPixelLenght /textviewWidth;;
    
    int textlineHeight = textview.getLineHeight ();
    if (textlineheight * numberOfLines > textviewHeight) {
        // text going beyond textviews height. Ellipsize the text using
        // TextUtils.ellipsize(params);     
    } 
    

    【讨论】:

    • 这并不能解决问题,但是当您不在 AppWidget 中时,这是可以的。
    • 使用 textPixelLenght /textviewWidth 作为 LineNum 是不准确的
    【解决方案3】:

    您需要知道字体的间距 -

    即,如果你知道它每行 20 个字符,并且你知道每一行都是 10px 高,那么你应该动态地将文本分配给 TextView:

    String toAdd = "";
    for (int i=0; i < textViewHeight; i+= textHeight) 
        toAdd += fullText.subString(i, i+charactersPerLine);
    

    【讨论】:

    【解决方案4】:

    解决您的问题的方法是创建一个自定义 TextView 并覆盖 TextView 的 onDraw() 方法。在那里你可以管理这个 TextView 处理这种情况的方式。

    【讨论】:

    • 它将在一个 appwidget 中。所以扩展 TextView 不好。
    【解决方案5】:

    使用字体的高度动态改变视图的高度,即

    如果你的文本是 10px 高,你的 TextView 只能是 10、20、30px 高。这并不要求您具有任何特定的字体样式。

    【讨论】:

    • 这听起来不错,但我不能在 appwidget 中做这些事情。我认为有一个 xml 属性。
    • 为什么不能在 AppWidget 中执行此操作?没有任何 XML 属性可以满足您的需求 - 如果您希望这种情况发生,恐怕您必须为此努力。
    • 更新 AppWidget 时,您不知道视图的大小。
    【解决方案6】:

    此解决方案将度量传递加倍,但由于这是一个没有子项的文本视图,因此影响不会太大。这对小部件也无济于事,因为它是自定义文本视图。对不起!

    public class ExactWrappingTextView extends TextView{
    
    public ExactWrappingTextView(Context context) {
        super(context);
    }
    public ExactWrappingTextView(Context context, AttributeSet attrs){
        super(context, attrs);
    }
    public ExactWrappingTextView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }
    
    private int measureHeight;
    private int extraSpace;
    
    //This doubles the measure pass for this view... but we have to know how 
        //high it would have been before we can pull off the right amount
    public void onMeasure(int width, int height){
        super.onMeasure(width, height);
        extraSpace = (getMeasuredHeight() - (getPaddingTop() + getPaddingBottom())) % getLineHeight();
        measureHeight = MeasureSpec.makeMeasureSpec(getMeasuredHeight() - extraSpace, MeasureSpec.AT_MOST);
        super.onMeasure(width, measureHeight);
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      相关资源
      最近更新 更多