Android 3: How to use custom colors

Source: Internet
Author: User

1. Define the color in color. xml

<?xml version="1.0" encoding="utf-8"?><resources>  <drawable name="darkgray">#808080FF</drawable>  <drawable name="white">#FFFFFFFF</drawable><drawable name="bule">#0000FF</drawable></resources>

 

2. Use it in Main. xml and directly display it through project running

 

<Absolutelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: autolink = "all" Android: text = "http://www.baidu.com:" Android: layout_x = "10px" Android: layout_y = "100px"/> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "http://www.baidu.com:" Android: textcolor = "@ drawable/darkgray" Android: Background = "@ drawable/White" Android: layout_x = "10px" Android: layout_y = "200px"/> </absolutelayout>

3. How to change the font color through program time

 

Resources resource=this.getBaseContext().getResources();//this.getResources();        Drawable drawable=resource.getDrawable(R.drawable.white);                        TextView tw=(TextView) this.findViewById(R.id.text);        tw.setBackgroundDrawable(drawable);       /* tw.setBackgroundColor(Color.WHITE);        tw.setBackgroundResource(R.drawable.white);*/

 

Conclusion: 1. the XML format is used to configure custom colors.
<Resources>
<Drawable name = "color name"> # color value </drawable>
</Resources>
2. Reference in XML
<Textview
....................
Android: textcolor = "@ drawable/darkgray"
Android: Background = "@ drawable/White"
.....................
/>

In string reference:
Android: Tex = "@ string/string name"

XML definition
Strings. xml
<Resources>
<String name = "string name"> string </string>
</Resources>
We can find that the definition formats of these resources are different from those of references only when the elements in the XML file are different,
You do not need to call the file name when using it, because these elements appear directly in the form of IDs in the r file.

3. Reference in the program
Drawable = resource. getdrawable (R. drawable. White );
Tw. setbackgrounddrawable (drawable );

4. Here are three ways to set the background color of textview.
Void setbackgroundcolor (INT color );
Void setbackgrounddrawable (drawable D );
Void setbackgroundresource (INT resid );
------------------------------------------------
Tw. setbackgrounddrawable (drawable); // reference the Custom Resource file.
Tw. setbackgroundcolor (color. White); // The values in the color class are directly used here.
Tw. setbackgroundresource (R. drawable. White); // This is called as an ID.

 

 

 

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.