This is the 7th day of my participation in the August More Text Challenge
I found that TextView was used frequently in my work, so I wrote a detailed explanation of the application of TextView. This is mainly about the use of TextView, not source code analysis.
Properties are explained below:
android:allowUndo
The default value is true (default support for undo). After connecting to the external keyboard, just press CTRL + Z to complete the undo. Currently only those over 23 support it.
android:autoLink
Automatically connect to the corresponding format, for example, you fill in the property value of “phone”, then when clicking the phone number, the page for dialing will automatically open; If you fill in “Email”, the page for developing email will be automatically displayed. Of course, if you have logged in your email software, there are altogether the following attributes:
The property name | value | instructions |
---|---|---|
phone | 4 | Make a phone call |
2 | Send E-mail | |
none | 0 | Nothing (default) |
web | 1 | Browse corresponding Webpage |
all | 15 | Automatic recognition of the above |
map | 8 | It’s been abandoned |
The median column is set when we use code control, except we don’t set it directly, and it’s taken from Linkify; Of course also can assign a value to: phone | web, it represents in the two choices; Only when the corresponding value is not one of the two is none, so here write phone | web equivalent to phone | web | none. The corresponding values are shown in the following table:
attribute | value | Corresponding constant |
---|---|---|
phone | 4 | Linkify.PHONE_NUMBERS |
2 | Linkify.EMAIL_ADDRESSES | |
web | 1 | Linkify.WEB_URLS |
all | 15 | Linkify.ALL |
map | 8 | Linkify.MAP_ADDRESSES |
Code controls use: setAutoLinkMask(int), of course in Kotlin:
textView? .autoLinkMask = Linkify.WEB_URLSCopy the code
It’s just that the code controls, which I’ve tested many times, don’t work, maybe they’re turned on the wrong way; Mixed text is also supported, which is acceptable if the text contains both linked text and plain text. For example, a website could look like this:Baidu: www.baidu.com
Or you can do thatBaidu: www.baidu.com \ n my Denver home page: https://juejin.cn/user/43636198216061/posts
That is, both addresses are matched at the same time, and both can be clicked. By default, text with links is colored differently than regular text, as shown below:If you want to change the color of the linked text, look at the following properties.
android:textColorLink
Link text color, can be set values refer to: set color in 1,2,3,5 mode. It’s pretty easy to know when a text contains a phone number and you want to click the phone number to make a call. Otherwise, you need to use an Intent to make a call.
android:autoSizeMaxTextSize
Sets the maximum resizable font size.
android:autoSizeMinTextSize
Sets the minimum resizable font size. Normally, by setting the font size in sp, the text will follow the system font size; You can adjust it by setting font size and Weight on your phone.
This is the setup on this page.
android:text
The content of the text can be written as text or the value defined in strings. XML. In addition, if it is a specified text, the official recommendation is to use the defined text instead of writing the dead text directly. The specific reason is that it is convenient for future internationalization, and sometimes the modification of the text does not need to be carried out in the code. Instead, you modify it directly in the strings.xml file.
Use plain text in code:
title.text = "Wu Jingyue"
Copy the code
You can use rich text in code that looks like this:
val ss = SpannableString("The world is in a state of flux.")
val span = ForegroundColorSpan(Color.parseColor("#ff0000"))
ss.setSpan(span, 5.9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
title.text = ss
Copy the code
Effect:For more rich text effects, see the link:SpannableString.
android:textAllCaps
Is the text displayed in all caps, with values of true and false? I feel that this attribute is rarely used, after all, most characters are used.
android:textColor
The color of the text is set to 1,2,3,5, and code:
title.setTextColor(ContextCompat.getColor(context, R.color.red))
title.setTextColor(ActivityCompat.getColor(context, R.color.red))
Copy the code
The two ways are actually one way; Where r.color. red is the color defined in colors.xml. If the value is directly hexadecimal, then:
title.setTextColor(Color.parseColor("#ff00ff"))
Copy the code
If you want to use the same font color as other text, do this:
title.setTextColor(other.currentTextColor)
Copy the code
Where other is another TextView text node. If you need to use arGB, you can do something like this:
title.setTextColor(Color.argb(0.5 f.1f.0f.0f))
Copy the code
This API can be used only if the SDK is 26 or greater, and the value range is [0.0-1.0], the type is Float, not [0-255].
You can also assign a hexadecimal value directly, such as 0x000000FF, meaning 0xAARRGGBB, like this:
title.setTextColor(0xffff00ff)
Copy the code
Except kotlin has the following error:This is normal in Java, but it does not mean that all values are not allowed, just when the value is too large, but because the first item indicates transparency and the ff cannot be filled in, this way kotlin will not be able to set completely opaque colors.
Refer to blog posts or tutorials
- SpannableStringBuilder
- SetTextColor Notes
- TextView