开发者

How to add Textviews next to each other and move them to the next line if they dont fit

开发者 https://www.devze.com 2023-04-06 22:39 出处:网络
I have 5 textviews, each textview has its own background and they are one next to the other, their sizes change depending on the amount of text that I put in them. I want to know if

I have 5 textviews, each textview has its own background and they are one next to the other, their sizes change depending on the amount of text that I put in them. I want to know if at any point they stop fitting because they reach the border of the parent. But not only that if they dont fit I want to b开发者_高级运维e able to add something like "click here to see more". So how do I detect how much space have they taken so far as I go adding the text to them? thanks


You can use TextUtils.ellipsize.
Maintain the actual text in a member. Call ellipsize with the text as parameter and set the returned text to the textview.
You can set a callback TextUtils.EllipsizeCallback which will be called when the text gets ellipsized.

TextUtils.EllipsizeCallback ellipsizeCallback = new TextUtils.EllipsizeCallback(){
     void ellipsized(int start, int end) {
         // enable the `click here to see more` button.
     }
}

...
CharSequence elipsizedText = ellipsize (mtext, mtxtpaint, 
                                        avail, TextUtils.TruncateAt.END , 
                                        preserveLength, ellipsizeCallback);
tv.setText(mtext);
0

精彩评论

暂无评论...
验证码 换一张
取 消