This article describes the text display and modification methods of Android programming and development. Share it for your reference, as follows:
1. Create a new Activity and Layout
First, create a new activity_main.xml in the layout folder. When creating a new project, you will generally create this xml file by default. The code is modified as follows:
activity_main.xml code
<RelativeLayout xmlns:andro xmlns:tools="/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: 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 file code as follows:
Code:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); TextView lblTitle=(TextView)findViewById(); ("This is what is displayed"); } }
Through the above method, you can modify the displayed text content in the TextView
2. Show the connected text
Show connection string
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); TextView lblTitle=(TextView)findViewById(); ("<a href=\"\">Baidu</>"); }
Modify the above code, then run and check the mobile phone interface, and find that this string is not automatically parsed in Html format, which means that the TextView mode does not support Html string parsing.
Parsing text with http
@Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); TextView lblTitle=(TextView)findViewById(); (); ("<a href=\"\">Baidu</>"); }
We can set a string with a connection through setAutoLinkMask, or use the following code:
Connection text display:
<RelativeLayout xmlns:andro xmlns:tools="/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: 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>
Attachment: Detailed explanation of Android's TextView attribute XML:
Attribute name Description
android:autoLink Sets whether when the text is a URL link/email/phone number/map, the text is displayed as a clickable link. Optional value (none/web/email/phone/map/all)
android:autoText If set, spelling correction of the input value will be automatically performed. There is no effect here, and it works when displaying the input method and inputting.
android:bufferType Specifies the text category obtained by getText() method. The option editable is similar to StringBuilder to append characters.
In other words, after getText, you can call the append method to set the text content. spannable can use styles in a given character area, see here 1 and here 2.
android:capitalize Set the capitalization type of English letters. There is no effect here, and you need to pop up the input method to see it. See EditView's property description.
android:cursorVisible Set the cursor to show/hide, and display by default.
android:digits Sets which characters are allowed to be entered. For example, "1234567890.+-*/%\n()"
android:drawableBottom outputs a drawable below the text, as shown in the picture. If a color is specified, the background of the text will be set to that color, and the latter will be overwritten when used in background.
android:drawableLeft outputs a drawable on the left side of the text, as shown in the picture.
android:drawablePadding Sets the interval between text and drawable (picture), and is used with drawableLeft, drawableRight, drawableTop, drawableBottom. It can be set to a negative number, and it has no effect when used alone.
android:drawableRight outputs a drawable on the right side of the text, as shown in the picture.
android:drawableTop outputs a drawable directly above the text, as shown in the picture.
android:editable Set whether it is editable. There is no effect here, see EditView.
android:editorExtras Set additional input data for text. Discuss it again in EditView.
android:ellipsize Sets how the control should be displayed when the text is too long. There are the following value settings: "start" - the ellipsis is displayed at the beginning; "end" - the ellipsis is displayed at the end; "middle" - the ellipsis is displayed in the middle; "marquee" - the ellipsis is displayed in the form of a marquee (animation moves horizontally)
android:freezesText Set the content of the text and the position of the cursor. See: here.
android:gravity Sets the text position. If set to "center", the text will be displayed in the center.
android:hint The text prompt information displayed when Text is empty, you can set the color of the prompt information through textColorHint. This property is used in EditView, but it can also be used here.
android:imeOptions additional function, sets the IME action in the lower right corner and the edit box related actions. For example, the lower right corner of actionDone will display a "complete" without setting a default entry symbol. This is explained in detail in EditView, and it is useless here.
android:imeActionId Sets the IME action ID. Let's explain it in EditView, you can read this post first: here.
android:imeActionLabel Sets the IME action tag. Make instructions in EditView.
android:includeFontPadding Sets whether the text contains extra white space at the top and bottom, the default is true.
android:inputMethod specifies the input method for text, and requires a fully qualified name (full package name). For example:, but an error is reported here and cannot be found.
android:inputType Sets the type of text to help the input method display the appropriate keyboard type. It will be explained in detail in EditView, but there is no effect here.
android:marqueeRepeatLimit When ellipsize specifies marquee, set the number of repeated scrolling times, and when set to marquee_forever, it means infinite times.
android:ems Set the width of the TextView to the width of N characters. The test here is a Chinese character width, as shown in the figure:
android:maxEms Set the width of the TextView to the width of the longest N characters. Override the ems option when used with ems.
android:minEms Set the width of the TextView to the shortest width of N characters. Override the ems option when used with ems.
android:maxLength limits the displayed text length, and the excess part will not be displayed.
android:lines Set the number of lines of the text. Set two lines to display two lines, even if the second line has no data.
android:maxLines Set the maximum number of lines displayed in the text, used in combination with width or layout_width, if the excess line wraps, the number of lines will not be displayed.
android:minLines Sets the minimum number of lines of text, similar to lines.
android:linksClickable Set whether the link clicks on the connection, even if autoLink is set.
android:lineSpacingExtra Set line spacing.
android:lineSpacingMultiplier Sets multiples of line spacing. Such as "1.2"
android:numeric If set, the TextView has a numeric input method. This is useless. The only effect after setting is that the TextView has a click effect. This property will be explained in detail in EdtiView.
android:password Display text with small dots "."
android:phoneNumber is set as the input method of the phone number.
android:privateImeOptions Set input method options, which is useless here and will be discussed further in EditText.
android:scrollHorizontally Set whether horizontal pull bars appear when the text exceeds the width of the TextView.
android:selectAllOnFocus If the text is selectable, let it get the focus instead of moving the cursor to the beginning or end position of the text. There is no effect after setting in TextView.
android:shadowColor specifies the color of the text shadow and needs to be used with shadowRadius. Effect:
android:shadowDx Sets the starting position of the shadow horizontal coordinates.
android:shadowDy Sets the starting position of the shadow vertical coordinates.
android:shadowRadius Sets the radius of the shadow. Set to 0.1 to become the font color, and generally the effect is better if set to 3.0.
android:singleLine Sets a single line display. If used with layout_width, when the text cannot be displayed all, it will be represented by "..." later. For example android:text="test_ singleLine" android:singleLine="true" android:layout_width="20dp" will only display "t...". If singleLine is not set or false, the text will be automatically wrapped
android:text set display text.
android:textAppearance Sets the text appearance. For example, "?android:attr/textAppearanceLargeInverse
"The reference here is a appearance that comes with the system, which means whether the system has this appearance, otherwise the default appearance will be used. The values that can be set are as follows: textAppearanceButton/textAppearanceInverse/textAppearanceLarge/textAppearanceLargeInverse/textAppearanceMedium/textAppearanceMediumInverse/textAppearanceSmall/textAppearanceSmallInverse
android:textColor Set text color
android:textColorHighlight The background color of the selected text, default to blue
android:textColorHint Sets the color of the prompt message text, the default is gray. Use with hint.
android:textColorLink The color of the text link.
android:textScaleX Sets the interval between texts, the default is 1.0f. Set the 0.5f/1.0f/1.5f/2.0f respectively as follows:
android:textSize Set the text size, recommend the unit of measurement "sp", such as "15sp"
android:textStyle Set glyphs [bold (bold) 0, italic (italic) 1, bolditalic (bold and oblique) 2] You can set one or more, separated by "|"
android:typeface Setting text fonts must be one of the following constant values: normal 0, sans 1, serif 2, monospace (monospace font) 3]
android:height Set the height of the text area, and supports unit of measurement: 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 Sets the width of the text area, and supports measurement units: px (pixels)/dp/sp/in/mm (mm). See here for the difference between layout_width.
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 be helpful to everyone's Android programming design.