TextView中的设置各行不同的宽度 [英] Set individual lines of TextView to different width

查看:311
本文介绍了TextView中的设置各行不同的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个包含两行文本一个TextView,第二行是比第一次短。

I'm trying to setup a TextView that contains two lines of text, the second line being shorter than the first one.

有没有办法来设置TextView的布局实例(通过TextView.getLayout()收购)截断第二行,或者是有,我是不知道的另一种方式?

Is there a way to set up TextView's Layout instance (acquired via TextView.getLayout()) to truncate the second line, or is there another way that I'm not aware of?

修改1

有是我的意图misinter pretations:我想设置的方式layouted一个TextView,我可以设置任意文本并根据以下情况的长度:
如果文本是足够长,以启动第二线,使得第一行一样宽TextView的内容宽度,但第二行应该是短,例如半宽。但应该有第二行,如果文本是足够长的时间。

There were misinterpretations of my intentions: I'm trying to set a textview layouted in a way, that i can set arbitrary text and depending on the length the following happens: If the text is long enough to start a second line, make the first line as wide as the textview's content width is, but the second line should be shorter, for example half as wide. But there should be a second line, if text is long enough.

我检查了抽象类的布局和它的子类,有一个函数

I checked out abstract class Layout and it's subclasses, there is a function

public int getLineEnd(int line)

用于返回行的最后一个字符的位置。一种方法可以设置文本,然后检查第二行有多宽layouted,然后将文本更改为较短的版本。不管怎么说,这似乎是一个哈克方式,这可以作出更清洁和更稳定的,如果我可以设置一个布局实例自己(通过setter方法​​,比如吸气

that returns the position of the last character for a line. A way could be to set the text, then check out how wide the second line is layouted and then change the text to a shorter version. Anyways, that seems to be a hacky way, which could be made much cleaner and more stable, if I could set a Layout instance myself (via a setter method, such as getter

TextView.getLayout().

有没有人设置自定义布局到TextView中的一个子类?

Has anyone set a custom Layout to a subclass of Textview?

编辑2 /我自己的解决方案

找到了解决办法:
现在我检查,如果第二行与被用于使第二线较短的原因所在区域重叠。如果是这样,我截断文本。
功能我用:

Found a solution: Now I'm checking if the second line overlaps with the region that was the reason for making the second line shorter. If so, I'm truncating the text. Functions I used:

TextView.getLineCount()
TextView.getLayout().getLineStart(int line)
TextView.getLayout().getLineEnd(int line)
TextView.getPaint().measureText()

也许这有助于未来的任何人。

Maybe that helps a future anyone.

如何设置自己的布局仍然问题仍然没有答案,但。

The question how to set an own layout still remains unanswered, though.

推荐答案

我遇到了同样的问题,这里是我的解决方案:

I met the same problem,here is my solution:

这里是code时,truncateText法计算文本长度并截断。

here is the code,the truncateText method to calculate the text length and truncate.

public class LastLineControlTextView extends TextView{

private int mMaxLines;
private float mLastLineRatio = 0.5f;
private float mLastLineWidth;
private float mAllowLength;

public LastLineControlTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mLastLineWidth = getWidth()*mLastLineRatio;
    mAllowLength = (mMaxLines-1)*getWidth()+mLastLineWidth;
    if(!TextUtils.isEmpty(getText())){
        truncateText(getText());
    }
}

@Override
public void setText(CharSequence text, BufferType type) {
    super.setText(text, type);
    if(mAllowLength==0)
        return;
    truncateText(text);
}

@Override
public void setMaxLines(int maxlines) {
    super.setMaxLines(maxlines);
    mMaxLines = maxlines;
}

private void truncateText(CharSequence text){
    float width = Layout.getDesiredWidth(text, getPaint());
    int end = text.length();
    while(width > mAllowLength){
        width = Layout.getDesiredWidth(text, 0, end, getPaint());
        end--;
    }
    if(end==text.length())
        return;
    CharSequence processed = text.subSequence(0, end);
    System.out.println("end:"+end+"processed:"+processed);
    setText(processed);
}
}

这篇关于TextView中的设置各行不同的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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