2012-03-17 59 views
8

扩展为多行的TextView似乎会自动展开以适合其最大宽度。我怎样才能防止这种情况发生?防止多行TextView不必要地扩展到其最大宽度

下面是一个例子布局(的test.xml)

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white"> 
<TextView 
android:background="@color/green" 
android:layout_alignParentRight="true" 
android:id="@+id/foo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:text="This is the message a much shorter message" 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textColor="@color/black" /> 
</RelativeLayout> 

TextView shown with maximum width

如果我在左边增加一个余量,它正确地缩小了的TextView的宽度(不重新格式化的内容),但是在给定TextView内容的情况下,边距的大小必须进行计算。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white"> 
<TextView 
android:layout_marginLeft="32dp" 
android:background="@color/green" 
android:layout_alignParentRight="true" 
android:id="@+id/foo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:gravity="right" 
android:text="This is the message a much shorter message" 
android:textAppearance="?android:attr/textAppearanceMedium" 
android:textColor="@color/black" /> 
</RelativeLayout> 

TextView show with margin

回答

0

使用android:padding="32dp" 这是帮助你有填充,如果你想从左边使用具有填充android:paddingLeft="32dp"

+1

问题不在于填充。我希望TextView实际上包装内容,而当它是多行时,它扩展到父视图的宽度。 – 2012-03-17 12:04:25

7

您可以使用自定义的TextView与重写的方法onMeasure(),您计算宽度:

public class WrapWidthTextView extends TextView { 

... 

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

    Layout layout = getLayout(); 
    if (layout != null) { 
     int width = (int)FloatMath.ceil(getMaxLineWidth(layout)) 
       + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     int height = getMeasuredHeight();    
     setMeasuredDimension(width, height); 
    } 
} 

private float getMaxLineWidth(Layout layout) { 
    float max_width = 0.0f; 
    int lines = layout.getLineCount(); 
    for (int i = 0; i < lines; i++) { 
     if (layout.getLineWidth(i) > max_width) { 
      max_width = layout.getLineWidth(i); 
     } 
    } 
    return max_width; 
} 

}

+0

+1000 :)谢谢! – 2015-04-10 16:23:21

0

我试过Vitaliy Polchuk的解决方案,它工作正常。而且我也想加入小的修正:

... 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    Layout layout = getLayout(); 
    if (layout != null) { 
     int width = (int) FloatMath.ceil(getMaxLineWidth(layout)) 
       + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
    } 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    // get already measured dimensions 
    int width = getMeasuredWidth(); 
    int height = getMeasuredHeight(); 
    setMeasuredDimension(width, height); 
} 
... 
0

如果你想使用@Vitaliy Polchuk(上回答)的建议,修改,只有这样或看到他在Why is wrap content in multiple line TextView filling parent?回答:

public class WrapWidthTextView extends AppCompatTextView { 
    public WrapWidthTextView(Context context) { 
     super(context); 
    } 

    public WrapWidthTextView(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public WrapWidthTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

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

     Layout layout = getLayout(); 
     if (layout != null) { 
      int width = (int) Math.ceil(getMaxLineWidth(layout)) 
        + getCompoundPaddingLeft() + getCompoundPaddingRight(); 
      int height = getMeasuredHeight(); 
      setMeasuredDimension(width, height); 
     } 
    } 

    private float getMaxLineWidth(Layout layout) { 
     float max_width = 0.0f; 
     int lines = layout.getLineCount(); 
     for (int i = 0; i < lines; i++) { 
      if (layout.getLineWidth(i) > max_width) { 
       max_width = layout.getLineWidth(i); 
      } 
     } 
     return max_width; 
    } 
} 

将此类插入到XML时重建项目。否则它不会找到这个类。