introduction
TextView is one of the most commonly used controls in Android, and it has many properties to set, such as font size, color, alignment, etc. But, you know the TextView'smaxEmsandmaxLengthIs there any difference in attributes? Both properties can limit the text length displayed by the TextView, but their effects are not the same. This article will introduce the meaning, usage and differences of these two attributes. I hope it will be helpful to you.
maxEms property
The maxEms property refers to the maximum width of the TextView, in units of em. em is a relative unit that represents the width of a character. For example, if the font size of the TextView is 16sp, then an em is 16sp. If the maxEms of the TextView is set to 10, the maximum width of the TextView is 10 ems, that is, 160sp. If the text length of the TextView exceeds this width, the TextView will automatically wrap the line, or display the ellipsize according to the ellipsize property.
The maxEms attribute usage is as follows:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World" android:maxEms="10"/>
Note that the maxEms property is only valid for TextViews with layout_width of wrap_content. If layout_width is match_parent or fixed value, the maxEms property will be ignored.
maxLength attribute
The maxLength attribute refers to the maximum number of characters displayed by the TextView. If the text length of the TextView exceeds this number of characters, the extra characters will be truncated, or the ellipsize will be displayed according to the ellipsize property.
The maxLength attribute is used as follows:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World" android:maxLength="5"/>
Note that the maxLength property will not affect the width of the TextView, but will only affect the display of the text. If you want the TextView to adapt to the width according to the text length, you can set layout_width to wrap_content.
The difference between maxEms and maxLength
From the above introduction, we can see that both maxEms and maxLength can limit the text length displayed by the TextView, but they have the following differences:
- maxEms limits the width of the TextView in units of em, while maxLength limits the number of characters displayed by the TextView in terms of the number of characters.
- maxEms is only valid for TextViews with layout_width of wrap_content, while maxLength is valid for any TextView.
- maxEms will affect the textView's newline and ellipsis display, while maxLength will only affect the text's truncation and ellipsis display.
- maxEms and maxLength can be used at the same time, but there may be conflicts between them. For example, if maxEms is set to 10 and maxLength is set to 20, then when the TextView displays 20 characters, it may exceed the width of 10 ems, resulting in line breaks or ellipses. vice versa.
Summarize
This article introduces the meaning, usage and differences of the maxEms and maxLength properties of Android TextView. Both properties can limit the text length displayed by the TextView, but they have different ways of acting and effects. When using these two properties, you need to pay attention to the possible conflicts between them and choose the appropriate properties according to actual needs.
The above is the detailed content of the difference between maxEms and maxLength attributes of Android TextView. For more information about the difference between Android TextView attributes, please pay attention to my other related articles!