1. Android textview control learning notes

Source: Internet
Author: User
Textview Control Learning

1. There are two ways to create a textview object:

1.1 create a textview object in the program;

Textview TV =NewTextview (This);


TV. settext ("hello ");


Setcontentview (TV );

1.2 used in XML layout files (this method is recommended)

<Textview

Android: Id ="@ + ID/TV"


Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"

Android: text ="Hello"

/>

We recommend that you use SP as the unit to set the font size.

DIP is used as the unit when setting the width or height attribute.

2 difference between dp sp and PX:

DP is dip, which is similar to sp. If it is set to indicate the length, height, and other attributes, you can use DP or sp. However, if you set the font, you need to use sp. DP is not related to density. SP is not only related to density, but also to scale. If the screen density is 160, DP and SP are the same as PX. 1dp = 1sp = 1px. However, if PX is used as the unit, if the screen size remains unchanged, the screen density changes to 320. the original textview width is set to 320 PX, And the 3.2-inch screen with a density of 160 is half shorter than the 3.2-inch screen with a density. However, if it is set to 160dp or 160sp, the system automatically sets the width attribute value to 320px. That is, 160*320/160. Among them, 320/160 is called the density proportional factor. That is to say, if DP or SP is used, the system will automatically convert according to the screen density change.

Px: the actual pixel of the screen.

 

3. Font color setting

3.1
Android: textcolor = "#00ff00"

3.2

Textview TV = (textview) findviewbyid (R. Id.TV);

TV. settext (HTML.Fromhtml("<Font color = blue> my android </Fon> Development Study Notes "));

The above code can change the color of partial text in the textview control.

3.3

String STR = "Welcome to Android development ";

 
Spannablestringbuilder style =NewSpannablestringbuilder (STR );


Style. setspan (NewForegroundcolorspan (color.Red), 0, 6, spannable.Span_exclusive_exclusive);


Style. setspan (NewForegroundcolorspan (color.Green), 6, 13, spannable.Span_exclusive_exclusive);


Style. setspan (NewForegroundcolorspan (color.Blue), 13,15, spannable.Span_exclusive_exclusive);

TV. settext (style );

4. Set hyperlinks:

Android: autolink: determines whether the text is displayed as a clickable link when the text is URL link, email, phone number, or map.

Android: autolink = "all"

5. Running Horse lights

Android: ellipsize: Set how to display the control when the text is too long. Set the following values:

"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 reality of running a horse lamp.

When set to Marquee, Android: marqueerepeatlimit sets the number of repeated scrolling times. When set to marquee_forever, the number of times is unlimited. Android: focusableintouchmode: Indicates whether to obtain the focus in touch mode. Android: whether the focusable control can obtain the focus.

Instance: <textview

Android: Id ="@ + ID/TV"

Android: autolink ="All"

Android: layout_width ="Fill_parent"

Android: layout_height ="Wrap_content"

Android: singleline ="True"

Android: textsize ="20sp"

Android: textcolor ="#00ff00"

Android: focusable ="True"

Android: ellipsize ="Marquee"

Android: marqueerepeatlimit ="Marquee_forever"

Android: focusableintouchmode ="True"

Android: text ="Welcome to learn about Android development. My blog address: http://blog.csdn.net/donggua%25my phone number 15823008888"

/>

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.