Android _ set the color and font of the control (001)

Source: Internet
Author: User
Tags getcolor
Set the color and font of the control.

Code:

Package com. oyzz. a7_6;

Import Android. App. activity;
/* The graphics. Color must be referenced to use the color. * object */
Import Android. Graphics. color;
Import Android. Graphics. typeface;

Import Android. OS. Bundle;
Import Android. View. view;

/* Widget. Button must be referenced to declare the use of the button object */
Import Android. widget. Button;

/* You must reference widget. textview to declare the use of the testview object */
Import Android. widget. textview;
Public class a7_6 extends Activity
{
Private button mbutton;
Private textview mtext;
Private int [] mcolors;
Private int colornum;
Private button fontbutton;

/** Called when the activity is first created .*/
@ Override

Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

/* Use the findviewbyid constructor to use main. xml and string. xml
Parameters of Button and textview */
Mbutton = (button) findviewbyid (R. Id. mybutton );
Mtext = (textview) findviewbyid (R. Id. mytext );
Fontbutton = (button) findviewbyid (R. Id. mybutton1 );

/* Declare and construct an integer array to store the color of the text to be used */
Mcolors = new int []
{
Color. Black, color. Red, color. Blue,
Color. Green, color. Magenta, color. Yellow
};
Colornum = 0;
// Obtain the color in the color. xml file.
Int B = getresources (). getcolor (R. drawable. Blue); // obtain the color in the configuration file.
Mbutton. settextcolor (B );
/* Use setonclicklistener to let the button listen to the event */
Mbutton. setonclicklistener (New View. onclicklistener ()
{
/* Use onclick to allow the user to click a button to change the text color */
Public void onclick (view V)
{
If (colornum <mcolors. length)
{
Mtext. settextcolor (mcolors [colornum]);
Colornum ++;
}
Else
Colornum = 0;
}
});

Fontbutton. setonclicklistener (New button. onclicklistener (){
Public void onclick (view v ){
Mtext. settypeface (typeface. createfromasset (getassets (), "fonts/handmadetypewriter. TTF"); // set the font
Fontbutton. settypeface (typeface. defaultfromstyle (typeface. italic); // you can set this parameter internally.
}
});
}
}

Main. xml:

<? XML version = "1.0" encoding = "UTF-8"?>

<! -- Layout uses a white background -->
<Linearlayout
Android: Id = "@ + ID/widget27"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Background = "@ drawable/White"
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
>
<! --
Use mytext as the ID and use string. xml
Textview_str numeric to set the text color to gray
-->
<Textview
Android: Id = "@ + ID/mytext"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/textview_str"
Android: textcolor = "@ drawable/darkgray"
>
</Textview>
<! -- Use mybutton as ID in string. xml
Button_str numeric
-->
<Button
Android: Id = "@ + ID/mybutton"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button_str"
>
</Button>
<Button
Android: Id = "@ + ID/mybutton1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "font"
>
</Button>
</Linearlayout>

Color. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Drawable name = "darkgray"> # 404040ff </drawable>
<Drawable name = "black"> #000 </drawable>
<Drawable name = "red"> # ff00ff </drawable>
<Drawable name = "green"> # 0ff0ff </drawable>
<Drawable name = "lightgray"> # c0c0c0ff </drawable>
<Drawable name = "white"> # ffffffff </drawable>
<Drawable name = "yellow"> # ffff33ff </drawable>
<Drawable name = "blue"> #00 FFFF </drawable>
<Drawable name = "gray"> # 808080ff </drawable>
<Drawable name = "Magenta"> # ff6699ff </drawable>
<Drawable name = "cyan"> #66 ffffff </drawable>
</Resources>

Strings. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, ex03_13 </string>
<String name = "app_name"> ex03_13 </string>
<String name = "textview_str"> turn to colorful neon lights </string>
<String name = "button_str"> press me </string>
</Resources>

Experience:

1. Use the code to set the color of the control:
Int B = getresources (). getcolor (R. drawable. Blue); // obtain the color in the configuration file.
Mbutton. settextcolor (B );

2. Set the font of the space:
Method 1: mtext. settypeface (typeface. createfromasset (getassets (), "fonts/handmadetypewriter. TTF"); // set the font
Note: 1. Ensure that the file must be in TTF format; 2. Put it in the assets/fonts directory; 3. If the corresponding font cannot be found, no error is reported, but it cannot be displayed during running.
Method 2: fontbutton. settypeface (typeface. defaultfromstyle (typeface. italic); // set it in the internal supported Mode

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/oyzz1988/archive/2009/11/23/4859136.aspx

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.