如何使textView准确包装其多行内容? [英] How to make textView wrap its multiline content exactly?

查看:57
本文介绍了如何使textView准确包装其多行内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何才能使textview准确地包裹此类文本?

How can I make the textview wrap such text exactly ?

android:width属性不是解决方案,因为文本是动态的.

android:width attribute is not a solution, because the text is dynamic.

期望的行为

|Adcs  |
|adscfd|

当前行为

|Adcs      |
|adscfd    |

这里是代码(TextView的样式仅定义诸如textColor,textSize,textStyle之类的东西).

Hereis the code (styles of TextViews only define things like textColor, textSize, textStyle).

<TextView
        android:id="@+id/text_title_holder"
        style="@style/TextBold.Black.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:maxWidth="100dp"
        android:maxLines="2"
        android:text="Adcs adscfd"
        android:gravity="left"
        android:visibility="visible" />

在mutiline TextView上的主题 wrap_content宽度没有很好的答案.

The topic wrap_content width on mutiline TextView has no good answer.

推荐答案

我遇到了这个问题,但没有在Internet上找到解决方案.我通过创建新的组件TightTextView来实现此目的,该组件将重新测量给定的文本,以防您指定了组件的maxWidth且Layout(文本)的宽度小于视图的测量宽度.

I have faced this problem and didn't find the solution in internet. I did this trick by creating the new component TightTextView that remeasures the given text in case you have specified the maxWidth of the component and the width of Layout (of text) is less that the measured width of the view.

package com.client.android.app.views;

import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Tightly wraps the text when setting the maxWidth.
 * @author sky
 */
public class TightTextView extends TextView {
    private boolean hasMaxWidth;

    public TightTextView(Context context) {
        this(context, null, 0);
    }

    public TightTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TightTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

        if (hasMaxWidth) {
            int specModeW = MeasureSpec.getMode(widthMeasureSpec);
            if (specModeW != MeasureSpec.EXACTLY) {
                Layout layout = getLayout();
                int linesCount = layout.getLineCount();
                if (linesCount > 1) {
                    float textRealMaxWidth = 0;
                    for (int n = 0; n < linesCount; ++n) {
                        textRealMaxWidth = Math.max(textRealMaxWidth, layout.getLineWidth(n));
                    }
                    int w = Math.round(textRealMaxWidth);
                    if (w < getMeasuredWidth()) {
                        super.onMeasure(MeasureSpec.makeMeasureSpec(w, MeasureSpec.AT_MOST),
                                heightMeasureSpec);
                    }
                }
            }
        }
    }


    @Override
    public void setMaxWidth(int maxpixels) {
        super.setMaxWidth(maxpixels);
        hasMaxWidth = true;
    }

    @Override
    public void setMaxEms(int maxems) {
        super.setMaxEms(maxems);
        hasMaxWidth = true;
    }
}

!!!只是将其移植到较早的android API时,cuz getMaxWidth()仅在API级别16起可用.

!!! Just did port it to older android APIs, cuz getMaxWidth() is only available since API level 16.

这篇关于如何使textView准确包装其多行内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆