Monodroid learning notes (5) -- change the definition of textview font color, background color, and style

Source: Internet
Author: User

In winform or webform, there are control classes. Although they are not in the same assembly, they all represent "controls. In monodroid, the control class is Android. Views. View. Anyone who has developed winform or webform knows that to display text on the interface, you only need to use the label control. In Android, textview is used.

There are two ways to set a textview text: one is to use the resource file to define a String constant and then reference it in the layout file. The second is to directly assign values using C # code in the background program.

Let's first try the first method. Add an item to the resources/values/strings. xml file as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <string name = "hello"> Hello world, click me! </String> <br/> <string name = "applicationname"> monodroidtest </string> <br/> <string name = "mytext">. text Set in XML </string> <br/> </resources> <br/> 

In the layout file main. axml, add a textview. The text references the newly added String constant, as shown below:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent"> <br/> <textview Android: text = "@ string/mytext" <br/> Android: layout_x = "30px" <br/> Android: layout_y = "0px" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/mytextview"/> <br/> </absolutelayout>

In activity1.cs, you only need to set main. axml to contentview:

Using system; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/>}< br/> 

After running, we can see a line of strings in the program, which is the same as what we set in strings. xml:

 

It is easier to use code to set the textview text. First, use the findviewbyid method in the oncreate method of the activity to find the textview from the layout file using the textview ID, then assign a value to the text:

Using android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> textview TV = findviewbyid <textview> (resource. id. mytextview); <br/> TV. TEXT = "this is the text set by the program"; <br/>}< br/> 

It is prompted that textview does not support HTML Tag output, so even if it is written as follows: TV. TEXT = "<a href =/" http://blog.csdn.net/ojlovecd/ "> dianjian's blog </a>"; the actual output does not become a hyperlink, but if Android is added to textview: autolink = "all", or use TV. autolinkmask = 1, if there is a URL (http: //) in the text, it can be converted to a hyperlink:

 

Next, we will try to change the font color and background color of textview. In the third article, we have finally tried to change the background color of the program to white by adding a color under the values file. XML to set the color constant. However, in actual design, the most common method is to use a program to control the background color of textview or other objects. Next, we designed two textview objects in advance in layout, and changed the text color and background color of textview in the oncreate method of activity in two ways.

Using system; <br/> using Android. APP; <br/> using Android. content; <br/> using Android. runtime; <br/> using Android. views; <br/> using Android. widget; <br/> using Android. OS; <br/> using monodroidtest. tabs; <br/> using Android. util; <br/> using Java. io; <br/> namespace monodroidtest <br/>{< br/> [activity (Label = "monodroidtest", mainlauncher = true)] <br/> public class activity1: activity <br/>{< br/> protected override void oncreate (bundle) <br/>{< br/> base. oncreate (bundle); <br/> setcontentview (resource. layout. main); <br/> setcontentview (resource. layout. main); <br/> textview TV1 = findviewbyid <textview> (resource. id. mytextview); <br/> tv1.text = "this is the text that uses drawable to set the background color"; <br/> tv1.setbackgrounddrawable (this. resources. getdrawable (resource. color. white); </P> <p> textview TV2 = findviewbyid <textview> (resource. id. mytextview2); <br/> tv2.text = "this is the text that uses settextcolor to set the text color"; <br/> tv2.settextcolor (Android. graphics. color. green); <br/>}< br/> 

 

So how can we get the string constants defined in strings. xml if we want to use them in the program? You can use the getstring method in activity:

Tv1.text = This. getstring (resource. String. mytext ). Note that if string. when the string constants in XML contain special characters such as single quotation marks, double quotation marks, and slash/, remember to use the Escape Character (/): <string name = "mytext"> what is this? // Text set by/'strings. xml/"</string>

Android can be set to adjust the scaling ratio with the window size, but even so, developers must still know the border of the mobile phone screen to avoid layout deformation problems. The method for obtaining the resolution of a mobile phone is very simple. The key is the displaymetrics application.

Textview TV2 = findviewbyid <textview> (resource. id. mytextview2); <br/> displaymetrics dm = new displaymetrics (); <br/> This. windowmanager. defaultdisplay. getmetrics (DM); <br/> tv2.text = string. format ("cell phone resolution: {0} × {1}", DM. widthpixels, DM. heightpixels); <br/> 

 

Is it too troublesome to specify the size and color of text one by one? Is there a way to specify these styles like CSS? In fact, in monodroid, you can also change the appearance of any objects in layout in style mode.

First, add a style. xml file under the values file as our style file, and then add two styles to it as an example:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <MCE: style name = "textstyle1"> <! -- <Br/> <item name = "Android: textsize"> 18sp </item> <br/> <item name = "Android: textcolor "> # ec9237 </item> </P> <p> --> </MCE: style> <style name = "textstyle1" mce_bogus = "1"> <item name = "Android: textsize "> 18sp </item> <br/> <item name =" Android: textcolor "> # ec9237 </item> <br/> </style> <br/> <MCE: style name =" textstyle2 "> <! -- <Br/> <item name = "Android: textsize"> 14sp </item> <br/> <item name = "Android: textcolor "> # ff7f7c </item> <br/> <item name =" Android: fromalpha "> 0.0 </item> <br/> <item name =" Android: top Alpha "> 0.0 </item> </P> <p> --> </MCE: style> <style name = "textstyle2" mce_bogus = "1"> <item name = "Android: textsize "> 14sp </item> <br/> <item name =" Android: textcolor "> # ff7f7c </item> <br/> <item name =" Android: fromalpha "> 0.0 </item> <br/> <item name =" Android: toalpha "> 0.0 </item> <br/> </style> <br/> </resources> 

Then, specify the style attribute in the layout file to apply the style defined in style. xml. If vs prompts you that the "style" feature is not declared under the style, do not worry about it and generate it directly.

<Textview Android: text = "@ string/mytext" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: id = "@ + ID/mytextview" <br/> style = "@ style/textstyle1" mce_style = "@ style/textstyle1"/> <br/> <textview Android: id = "@ + ID/mytextview2" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_y = "30px" <br/> style = "@ style/textstyle2" mce_style = "@ style/textstyle2"/> <br/> 

 

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.