Reference Drawable color constant and background color

Source: Internet
Author: User

Store the defined color code in resources with the name of drawable in advance. This is a good habit to learn how to develop Android programs, just like the String constant, the color can also be colors under the values file under the res directory in advance. the format defined in the xml file is as follows: [html] <span style = "font-size: 18px; "> <drawable name = color_name> color_value </drawable> </span> the following example uses two methods to use the defined constant. Method 1: use it in an xml file by referencing the following method: [html] <span style = "font-size: 18px;"> android: background = "@ drawable/color_name" </span> Method 2: Use java code for calling. The following code is required: [html] <span style = "font-size: 18px; "> // method for obtaining Resources resources Resources = getBaseContext (). getResources (); Drawable HippoDrawable = resources. getDrawable (R. drawable. color_name); </span> The following is an example using both methods: 1. run as follows: 2. implementation Code: 2.1 layout file [html <span style = "font-size: 18px; "> <? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android =" http://schemas.android.com/apk/res/android "Android: orientation =" vertical "android: layout_width =" fill_parent "android: layout_height =" fill_parent "android: background =" @ drawable/red "> <TextView android: id = "@ + id/myTextView01" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "@ string/str_textview01"/> <TextView android: id = "@ + id/myTextView02" android: layout_width = "fill_parent" android: layout_height = "wrap_co Ntent "android: text =" @ string/str_textview02 "/> </LinearLayout> </span> 2.2 Color Resource definition file [html] <span style =" font-size: 18px; "> <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <drawable name = "darkgray"> # 8080ff </drawable> <drawable name = "white"> # FFFFFFFF </drawable> <drawable name = "red"> # FF0000 </drawable> </resources> </span> 2.3 Main program file [java] <span style = "font-size: 18px; "> public class EX03_03 extends Activity {private TextView mTextView01; private TextView mTextView02;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle sav EdInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mTextView01 = (TextView) findViewById (R. id. myTextView01); mTextView01.setText ("I am the David text that applies the Drawable background color. "); // Method for obtaining Resources resources Resources = getBaseContext (). getResources (); Drawable HippoDrawable = resources. getDrawable (R. drawable. white); mTextView01.setBackgroundDrawable (HippoDrawable); mTextView02 = (TextView) findViewById (R. id. myTextView02); mTextView02.setTextColor (Color. MAGENTA); // The Color constants provided by the Color class are as follows: // Constants // int BLACK // int BLUE // int CYAN // int DKGRAY // int GRAY // int GREEN // int LTGRAY // int MAGENTA // int RED/ /int TRANSPARENT // int WHITE // int YELLOW }}</span>

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.