This article illustrates the TextView text display and modification method of Android programming development. Share to everyone for your reference, specific as follows:
A. A new activity and Layout
First create a new activity_main.xml in the Layout folder, and when you create a new project, you will typically create this XML file by default, modifying its code as follows:
Activity_main.xml Code
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_ Horizontal_margin "
android:paddingright=" @dimen/activity_horizontal_margin "
android:paddingtop=" @dimen /activity_vertical_margin "
tools:context=". Mainactivity ">
<textview
android:id=" @+id/lbltitle "android:layout_width=" Wrap_content "
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_ Alignparenttop= "true"
android:layout_marginleft= "88DP"
android:layout_margintop= "51DP"
android: text= "TextView"/>
</RelativeLayout>
Modify the Mainactivity.java file code as follows:
Mainactivity.java Code:
public class Mainactivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView lbltitle= (TextView) Findviewbyid (r.id.lbltitle);
Lbltitle.settext ("This is what is displayed");
}
The above method can be used to modify the display text content in TextView
Two. Show connected text
Show Connection string
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView lbltitle= (TextView) Findviewbyid (r.id.lbltitle);
Lbltitle.settext ("<a href=\" http://www.baidu.com\ "> Baidu </>");
}
Modify the code as above, and then run look at the phone interface, found that the HTML format does not automatically resolve this string, indicating that the TextView mode is not supported by HTML string parsing
Parsing text with HTTP
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
TextView lbltitle= (TextView) Findviewbyid (r.id.lbltitle);
Lbltitle.setautolinkmask (Linkify.all);
Lbltitle.settext ("<a href=\" http://www.baidu.com\ "> Baidu </>");
}
We can set the string with the connection by Setautolinkmask, or use the following code:
Connection Text display:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_ Horizontal_margin "
android:paddingright=" @dimen/activity_horizontal_margin "
android:paddingtop=" @dimen /activity_vertical_margin "
tools:context=". Mainactivity ">
<textview
android:id=" @+id/lbltitle "android:layout_width=" Wrap_content "
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_ Alignparenttop= "true"
android:layout_marginleft= "88DP"
android:layout_margintop= "51DP"
android: autolink= "All"
android:text= "TextView"/>
</RelativeLayout>
Attached: Android's TextView attribute xml detailed:
The
Property name Description
android:autolink Whether the text appears as a clickable link when the text is a URL link/email/phone number/map. Optional value (none/web/email/phone/map/all)
android:autotext If set, the spelling correction for the input value is automatically performed. No effect here, it works when the input input is displayed. The
android:buffertype specifies the type of text that the GetText () method obtains. Option editable is similar to StringBuilder can append characters,
that is, GetText can call the Append method to set the text content. Spannable can use styles in a given character area, see here 1, here 2.
Android:capitalize sets the English letter capitalization type. No effect here, you need to eject the input method to see it, see editview this property description.
Android:cursorvisible set cursor for show/hide, default display.
The Android:digits setting allows you to enter which characters. such as "1234567890.+-*/%\n ()"
Android:drawablebottom prints a drawable below the text, such as a picture. If you specify a color, the text background is set to the color, and the latter is overwritten with background.
Android:drawableleft prints a drawable on the left side of the text, such as a picture.
Android:drawablepadding sets the spacing between text and drawable (picture), and is used in conjunction with Drawableleft, Drawableright, Drawabletop, Drawablebottom, Can be set to a negative number, with no effect alone.
Android:drawableright prints a drawable on the right side of the text, such as a picture.
Android:drawabletop outputs a drawable, such as a picture, above the text.
Whether the android:editable setting is editable. There is no effect here, see EditView.
Android:editorextras sets the additional input data for the text. Discuss it again in EditView.
Android:ellipsize sets how the control should appear when the text is too long. The following values are set: the "Start"-– ellipses are displayed at the beginning; "End"--ellipses are displayed at the ends; "Middle"--ellipses are displayed in the middle; "Marquee"--Display as a marquee (animation moves horizontally)
Android:freezestext sets the contents of the saved text and the position of the cursor. See also: here.
Android:gravity sets the text position, as set to "center," and the text is centered.
Android:hint text message displayed when the text is empty, you can set the color of the hint message by Textcolorhint. This property is used in EditView, but can also be used here.
Android:imeoptions additional function, set the lower right corner IME action related to the edit box action, such as Actiondone in the lower right corner will display a "finish" instead of setting the default is a carriage return symbol. This is explained in detail in the EditView, which is useless here.
Android:imeactionid Sets the IME action ID. In EditView again to do the explanation, you can read this post: here.
Android:imeactionlabel Sets the IME action label. Make a note in EditView.
Android:includefontpadding sets whether the text contains extra white space at the top and bottom, and defaults to true.
ANDROID:INPUTMETHOD specifies the input method for text, requiring a fully qualified name (the full package name). For example: Com.google.android.inputmethod.pinyin, but here the error is not found.
Android:inputtype sets the type of text to help the input method display the appropriate keyboard type. In EditView, there is no effect.
Android:marqueerepeatlimit sets the number of repeat scrolling when ellipsize specifies marquee, which is infinite when set to Marquee_forever.
Android:ems sets the width of the TextView to n characters. This test is for a character width of Chinese characters, as shown in figure:
Android:maxems sets the width of the textview to a maximum of n characters. Override the EMS option when used in conjunction with EMS.
Android:minems sets the width of the textview to a minimum of n characters. Override the EMS option when used in conjunction with EMS.
Android:maxlength limits the length of text that is displayed and is not displayed beyond the section.
Android:lines sets the number of lines of text, setting two lines to display two lines, even if there is no data in the second row.
Android:maxlines sets the maximum number of lines of text to display, in conjunction with the width or layout_width, which exceeds the number of wrapping lines, and does not show up.
Android:minlines sets the minimum number of lines of text, similar to lines.
Android:linksclickable set the link to click on the connection, even if setting the Autolink.
Android:linespacingextra sets the line spacing.
Android:linespacingmultiplier sets the multiple of line spacing. such as "1.2"
Android:numeric if it is set, the TextView has a numeric input method. This is useless, the only effect after setting is TextView has a click Effect, this property will be detailed in Edtiview.
Android:password with a small dot "." Display text
Android:phonenumber is set to the way the phone number is entered.
Android:privateimeoptions setting Input method options, which are useless here, will be discussed further in EditText.
android:scrollhorizontally If the text is set to exceed the width of the TextView, the horizontal pull bar appears.
Android:selectallonfocus If the text is selectable, let him get focus instead of moving the cursor to the beginning or end of the text. No effect after setting in TextView.
ANDROID:SHADOWCOLOR Specifies the color of the text shadow, which needs to be used with Shadowradius. Effect:
ANDROID:SHADOWDX sets the position of the shadow's horizontal coordinates to begin.
Android:shadowdy sets the position of the shadow portrait coordinates to begin.
Android:shadowradius sets the radius of the shadow. Set to 0.1 to become the color of the font, the general set to 3.0 the effect is better.
Android:singleline set up a single-line display. If used with Layout_width, when the text cannot be all displayed, the following is indicated by "...". such as android:text= "Test_ singleline" android:singleline= "true" android:layout_width= "20DP" will only show "t ...". If you do not set Singleline or false, the text wraps automatically.
Android:text sets the display text.
Android:textappearance sets the appearance of the text. such as "? Android:attr/textappearancelargeinverse
"Here is a reference to the system's own appearance,?" Indicates whether the system has this appearance, otherwise the default appearance is used. The values you can set are as follows: textappearancebutton/textappearanceinverse/textappearancelarge/textappearancelargeinverse/ Textappearancemedium/textappearancemediuminverse/textappearancesmall/textappearancesmallinverse
Android:textcolor Set Text color
Android:textcolorhighlight the background of the selected text, the default is blue
Android:textcolorhint sets the color of the hint message text, which defaults to Gray. Used in conjunction with hint.
Android:textcolorlink the color of the text link.
Android:textscalex sets the spacing between text, which defaults to 1.0f. The 0.5f/1.0f/1.5f/2.0f effects are set separately as follows:
Android:textsize set the text size, recommend the unit of measure "SP", such as "15SP"
Android:textstyle set glyph [Bold (bold) 0, Italic (italic) 1, bolditalic (Bold and oblique) 2] can be set one or more, with "|" Separated
Android:typeface sets the text font, which must be one of the following constant values: normal 0, sans 1, serif 2, monospace (equal width font) 3]
Android:height set the height of the text area, support unit of measure: px (pixel)/dp/sp/in/mm (mm)
Android:maxheight sets the maximum height of the text area
Android:minheight sets the minimum height of the text area
Android:width sets the width of the text area, which supports the unit of measure: px (pixel)/dp/sp/in/mm (mm), and layout_width difference look here.
Android:maxwidth sets the maximum width of the text area
Android:minwidth sets the minimum width of the text area
I hope this article will help you with the Android program.