Android TextView Control Learning from scratch

Source: Internet
Author: User

TextView is a control that can be used to display strings.
Class Structure:
 
 
Details about TextView attributes
Attribute
Details
Android: autoLink
Set whether to display clickable links when the text is URL link, email, phone number, or map. Optional values (none/web/email/phone/map/all)
Android: autoText
If this parameter is set, the spelling of the input value is automatically corrected. There is no effect here. It works when the input method is displayed and entered.
Android: bufferType
Specifies the text category obtained by getText. The editable option is similar to the append character of StringBuilder. That is to say, after getText, you can use the append method to set the text content. Spannable can use styles in a given character area. For more information, see 1. 2.
Android: capitalize
Set uppercase letters. There is no effect here. The input method needs to be popped up to see it. For details, see EditView this attribute description.
Android: cursorVisible
Set the cursor to show/hide. The cursor is displayed by default.
Android: digits
Specifies the characters that can be entered. For example, "1234567890. +-*/% ()"
Android: drawableBottom
Output A drawable in the lower part of text. If a color is specified, the background of the text is set to this color, and the latter is overwritten with the background.
Android: drawableLeft
Output A drawable on the left of text.
Android: drawablePadding
Set the interval between text and drawable. It can be used with drawableLeft, drawableRight, drawableTop, and drawableBottom. It can be set to negative.
Android: drawableRight
Output A drawable on the Right of text.
Android: drawableTop
Output A drawable on the top of the text.
Android: editable
Set whether to edit.
Android: editorExtras
Set additional input data for text.
Android: ellipsize
Set how to display the control when the text is too long. You can set the following values: "start" -- ellipsis () at the beginning, "end" -- ellipsis () at the end, and "middle" -- ellipsis () in the middle; "marquee"-displayed as a marquee (horizontal movement of the animation)
Android: freezesText
Set the content of the saved text and the cursor position.
Android: gravity
Set the text location. For example, if it is set to "center", the text is displayed in the center.
Android: hintText
Text prompt information displayed when it is null. You can use textColorHint to set the color of the prompt information. This attribute is used in EditView, but it can also be used here.
Android: imeOptions
Additional function: sets the actions related to the IME action and the edit box in the lower-right corner. For example, a "finish" is displayed in the lower-right corner of actionDone, without setting a carriage return symbol by default. This is described in detail in EditView, Which is useless here.
Android: imeActionId
Set the IME action ID.
Android: imeActionLabel
Set IME action labels
Android: includeFontPadding
Set whether the text contains additional white space at the top and bottom. The default value is true.

Android: inputMethod
Specify the Input Method for text. You must specify the full package name ). For example, com. google. android. inputmethod. pinyin, but the error cannot be found.
Android: inputType
Set the text type to help the input method display the appropriate keyboard type. Detailed description in EditView, no effect here.
Android: linksClickable
Set whether to click the connection even if autoLink is set.
Android: marqueeRepeatLimit
When ellipsize specifies marquee, it sets the number of repeated scrolling times. When it is set to marquee_forever, it indicates unlimited times.
Android: EMS
Set TextView width to N characters. The test here is a Chinese character width.

Android: maxEms
Set the TextView width to a maximum of N characters. Overwrite the EMS option when used with EMS.
Android: minEms
Set TextView width to the width of N characters at the minimum. Overwrite the EMS option when used with EMS.
Android: maxLength
Limit the length of the displayed text. The excess part is not displayed.
Android: lines
Set the number of lines of text, and set the two lines to display the two lines, even if the second row has no data.
Android: maxLines sets the maximum number of lines displayed in text. It is used in combination with width or layout_width. If the line exceeded is automatically wrapped, the line exceeded is not displayed.
Android: minLines
Sets the minimum number of lines of text, similar to lines.
Android: lineSpacingExtra
Set row spacing
Android: lineSpacingMultiplier
Set the line spacing multiple. For example, "1.2"
Android: numeric
If this parameter is set, the TextView has a numeric input method. This is useless. The only effect after setting is that TextView has a click effect. This attribute is described in detail in EdtiView.
Android: password
Show text in small dots "."
Android: phoneNumber
Set it to the telephone number input method.
Android: privateImeOptions
It is useless to set the input method options, which will be further discussed in EditText.

Android: scrollHorizontally
Set whether to display a horizontal bar when the text exceeds the TextView width.

Android: selectAllOnFocus
If the text is selectable, let him get the focus instead of moving the cursor to the start or end of the text. After being set in TextView, there is no effect.
Android: shadowColor
Specifies the color of the text shadow, which must be used with shadowRadius.
Android: shadowDx
Set the start position of the Shadow horizontal coordinate.
Android: shadowDy
Set the start position of the Shadow vertical coordinate.
Android: shadowRadius
Set the shadow radius. If it is set to 0.1, it becomes the font color. Generally, it is better to set it to 3.0.
Android: singleLine
Set single row display. If it is used with layout_width, when the text cannot be fully displayed, "…" is used later. . For example, android: text = "test _ singleLine"
Android: singleLine = "true" android: layout_width = "20dp" only "t…" is displayed ...". If singleLine is not set or is set to false, the text will wrap automatically.
Android: text
Set the display text.
Android: textAppearance
Set text appearance
Android: textColor
Set text color
Android: textColorHighlight
The background color of the selected text. The default color is blue.
Android: textColorHint
Set the color of the prompt text. The default value is gray. Used with hint.

Android: textColorLink
Text Link color
Android: textScaleX
Set the text interval. The default value is 1.0f.
Android: textSize
Set the text size. The recommended measurement unit is "sp", for example, "15sp"
Android: textStyle
Set one or more [bold (bold) 0, italic (italic) 1, and bolditalic (bold and oblique) 2] separated by "| ".
Android: typeface
Set the text font, which must be one of the following constant values: normal 0, sans 1, serif 2, monospace (same width font) 3]
Android: height
Sets the height of the text area. supported measurement units: px (pixels)/dp/sp/in/mm (mm)
Android: maxHeight
Set the maximum height of the text area
Android: minHeight
Set the minimum height of the text area
Android: width
Set the width of the text area. The unit of measurement is px (pixel)/dp/sp/in/mm (mm). The difference between the unit of measurement and layout_width is shown here.
Android: maxWidth
Set the maximum width of the text area
Android: minWidth
Set the minimum width of the text area
 
 
 
 
 
 
 
How to change the color of the text displayed on the TextView control:
1.

Android: textColor = "#00FF00"
 
Android: text = "Hello, glad to meet you. My blog is http://blog.csdn.net/jiahui524. Phone number: 15580974038. Email: 272570596@qq.com "/>

 
 
In this way, all text is set to a color.


 
2. Change through code
 

TextView TV = (TextView) findViewById (R. id. TV );
 
 
 
TV. setText (Html. fromHtml ("Hello, <font color = 'red'> testtt </font> "));

 
This setting allows us to use the HTML knowledge we know to effectively control the color of text.
Note: In this method, you must first specify the android: id attribute in the TextView Tag:

<TextView
 
Android: id = "@ + id/TV"
 
Android: layout_width = "fill_parent"
 
Android: layout_height = "wrap_content"
 

 
/>

In this way, it will be automatically generated in the R. java file:

Public static final class id {
 
Public static final int TV = 0x7f050000;
 
}

 
 
3.

TextView TV = (TextView) findViewById (R. id. TV );
 
String str = "Hello, glad to meet you. My blog is http://blog.csdn.net/jiahui524. Phone number: 15580974038. Mailbox: 272570596@qq.com ";
 
SpannableStringBuilder style = new SpannableStringBuilder (str );
 
Style. setSpan (new ForegroundColorSpan (Color. RED), 0, 6, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
 
Style. setSpan (new ForegroundColorSpan (Color. GREEN), 6, 21, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
 
Style. setSpan (new ForegroundColorSpan (Color. BLUE), 21, 34, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
 
TV. setText (style );

 
This method also controls the color of text displayed between different fields:
 
 
Set the hyperlink mode:
 

<TextView android: id = "@ + id/TV"
 
Android: layout_width = "fill_parent"
 
Android: layout_height = "wrap_content"
 
Android: textColor = "#00FF00"
 
Android: text = "Hello, glad to meet you. My blog is http://blog.csdn.net/jiahui524. Phone number: 15580974038. Email: 272570596@qq.com"
 
Android: autoLink = "phone"
 
/>

Android: autoLink = "phone"
The values include web, phone, email, none, and all.
 

Author: jiahui524 Column

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.