How do I detect whether the text in a TextView
is on one line or not.
if it extends past one line, i want to use 128dp
Toolbar
if its on a single line, i want to use 48dp
You can set the android:singleLine
attribute to true
.
Refer to this documentation
如果返回1,则文本为单行,如下所示
int lineCount=mTextView.getLineCount();
Try this:
if(yourTv.getLineCount() > 1) {
yourTv.setTextSize();
else {
yourTv.setTextSize();
}
You can use StaticLayout
TextPaint textPaint = new TextPaint();
textPaint.setTypeface(textView.getTypeface());
textPaint.setTextSize(textView.getTextSize());
textPaint.setTextAlign(Paint.Align.ALIGN_NORMAL);
StaticLayout staticLayout = new StaticLayout(textString, textPaint, widthTextView, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, true);
if (1 == staticLayout.getLineCount()) {
//do smth
} else {
//do smth else
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.