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

查看:20
本文介绍了如何使 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    |

这里是代码(TextViews 的样式只定义了诸如 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" />

主题 wrap_content width on mutiline TextView 没有好的答案.

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

推荐答案

我遇到了这个问题,在网上没有找到解决方案.我通过创建新组件 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,因为 getMaxWidth() 仅在 API 级别 16 之后可用.

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

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

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