为什么在多行的TextView包装内容物填充母?内容、TextView

由网友(空心者)分享简介:我必须设置为多行文本视图的android:layout_width =WRAP_CONTENT,当呈现,作为母公司的所有可用的宽度。当文本可以在一行中, WRAP_CONTENT 工作正常,但在两个或多个行,文中观点似乎符合父母的宽度,留什么样子的填充上任何一方。 由于文本无法在一行,是文本视图假设,要求所有可用的宽度...

我必须设置为多行文本视图的android:layout_width =WRAP_CONTENT,当呈现,作为母公司的所有可用的宽度。当文本可以在一行中, WRAP_CONTENT 工作正常,但在两个或多个行,文中观点似乎符合父母的宽度,留什么样子的填充上任何一方。

由于文本无法在一行,是文本视图假设,要求所有可用的宽度是多少?我想以期在尽可能小的尺寸为界。

任何想法?

有关引用,这里是布局定义:

 <的TextView
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerInParent =真
    机器人:单线=假
    机器人:TEXTSIZE =24sp
    机器人:TEXTSTYLE =黑体
    机器人:文字颜色=@色/白
    机器人:重力=center_horizo​​ntal
/>
 
iOS 账号密码自动填充与添加

解决方案

我有同样的问题也... 您可以使用自定义的TextView与重写的方法onMeasure(),您计算宽度:

 公共类WrapWidthTextView扩展TextView的{

...

@覆盖
保护无效onMeasure(INT widthMeasureSpec,诠释heightMeasureSpec){
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);

    布局布局= getLayout();
    如果(布局!= NULL){
        INT宽度=(int)的FloatMath.ceil(getMaxLineWidth(布局))
                + getCompoundPaddingLeft()+ getCompoundPaddingRight();
        INT高= getMeasuredHeight();
        setMeasuredDimension(宽度,高度);
    }
}

私人浮动getMaxLineWidth(版面布局){
    浮max_width = 0.0;
    INT线= layout.getLineCount();
    的for(int i = 0; I<线;我++){
        如果(layout.getLineWidth(ⅰ)> max_width){
            max_width = layout.getLineWidth(ⅰ);
        }
    }
    返回max_width;
}
}
 

I have a multi line text view set to android:layout_width="wrap_content" that, when rendered, takes all available width of the parent. When the text can fit on one line, the wrap_content works fine, but at two or more lines, the text view seems to match the parents width, leaving what looks like padding on either side.

Because the text can not fit on one line, is the text view assuming to require all available width? I want the view to be bounded by the smallest possible dimensions.

Any ideas?

For reference, here is layout definition:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:singleLine="false"
    android:textSize="24sp"
    android:textStyle="bold"
    android:textColor="@color/white"
    android:gravity="center_horizontal"
/>

解决方案

I had the same problem also... You may use custom TextView with overridden method onMeasure() where you calculate the width:

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;
}
}

阅读全文

相关推荐

最新文章