Android Resource Details (i) color, string, bitmap resources

Source: Internet
Author: User

Color Resources

The definition of a color value is defined by an RGB primary color and an alpha value. The beginning of a color value definition is a

The pound sign (sentence, followed by the alpha-red-green-blue format. For example:

#RGB

#ARGB

#RRGGBB

#AARRGGBB

Color resources are located under the Value folder, create a new. xml file, add code inside it

<?xml version= "1.0" encoding= "Utf-8"?>

<resources>

<color name= "Hong" > #ff0000 </color>

<color name= "Huang" > #00ff00 </color>

<color name= "LV" > #0000ff </color>

</resources>


To reference a color resource in a resource file:

<textview android:id= "@+id/text1" android:layout_width= "match_parent" android:layout_height= "wrap_content "Android:text=" @string/str1 "android:textcolor=" @color/huang "/>

Add the code as above in the layout file, the reference format is "@ Color resource filename/color_name" (@color/huang)


In. Java code, reference the color resource, add the following code in. java

public class Mainactivity extends Activity {@Override protected void onCreate (Bundle savedinstancestate) {Super    . OnCreate (Savedinstancestate);    Setcontentview (R.layout.activity_main);    This.getwindow (). Setbackgrounddrawableresource (r.color.lv); Set background color//use R.color.color_name (R.COLOR,LV) to refer to//...}

string Resource


String resource is located under the Value folder, create a new. xml file, add code inside it

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >helloabc</string > <string name= "hello_world" >hello world!</string> <string name= "Action_settings" >Settings< /string><string name= "str1" >hello android!! </string></resources>


To reference a string resource in a resource file:

<textview android:id= "@+id/text1" android:layout_width= "match_parent" android:layout_height= "wrap_content "Android:text=" @string/str1 "<!--str1 as defined string resource--android:textcolor=" @color/huang "/>

referencing string resources in. Java:

Using functions

GetString (R.string.string_name). toString ();

eg

String string = GetString (R.STRING.STR1). toString ();


Bitmap Resource


drawable resources are pictures or color resources that are used primarily to draw screens through Resources.get

The Drawable () method is obtained. drawable resources are divided into three categories: Bitmap File (bitmap), Color drawable

(color), Nine-patch image (nine pictures). The use of commonly used bitmap files is only described here.

The bitmap files supported in Android are PNG, JPG, and GIF.




Copy the picture to the res/drawable file, refresh the project, update the item automatically, and get the bitmap ID, as you can see in R.java drawable.

Ps: The name of the picture can only be a-z1-9.png, cannot appear larger letters


To reference a bitmap in a layout file:

<imageview android:id= "@+id/pic1" android:layout_width= "wrap_content" android:layout_height= "wrap_content "Android:src=" @drawable/prenren "/>

As in the code, refer to the picture source in android:src format "@drawable/picture_name" ("@drawable/prenren")

To refer to a bitmap as an icon in a menu file:

<item android:title= "Edit"

android:icon= "@drawable/prenren"

</item>


In Java code, you can reference it in the following ways:

ImageView Myimageview = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Resources r=getresources ();//drawable Instance drawable d=r.getdrawable (r.drawable.picture_name) obtained through resources;//settings The Imagedrawable property of the ImageView displays the picture maimageview.setimagedrawable (d);

can also be combined to write:

This.getresources (). getdrawable (R.drawable.prenren);




This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1614844

Android Resource Details (i) color, string, bitmap resources

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.