A014-values Resources

Source: Internet
Author: User
Tags getcolor

About the resource files used in Android, the previous land continued to introduce a lot, this lesson to you about the use of Android "value resources."

You can see the above, in the values folder contains a lot of XML files, each file represents a value type, which defines the specific type of resources.

Take a look at what resource files can be defined under values:

Bool

Defining a bool value in Bools.xml

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <bool        name="bool_name"        >[true | false]</bool></resources>

eg
Save path: Res/values/bools.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <bool name="screen_small">true</bool>    <bool name="adjust_view_bounds">true</bool></resources>

Use in Java:

Resources res = getResources();boolean screenIsSmall = res.getBoolean(R.bool.screen_small);

Used in XML:

<ImageView    android:layout_height="fill_parent"    android:layout_width="fill_parent"    android:src="@drawable/logo"    android:adjustViewBounds="@bool/adjust_view_bounds" />
Color

Defining color values in an XML file
Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <color        name="color_name"        >hex_color</color></resources>

eg
Save path: Res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?><resources>   <color name="opaque_red">#f00</color>   <color name="translucent_red">#80ff0000</color></resources>

Use in Java:

Resources res = getResources();int color = res.getColor(R.color.opaque_red);

Used in XML:

<TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:textColor="@color/translucent_red"    android:text="Hello"/>
Dimension

Defining dimension values in XML

Dimensions can be used in the following units:
DP-density-independent pixels, mainly applied to spacing in layouts
SP-Similar to DP, main application and font size
PT-The screen physical length unit, which represents a point, is the physical size of the screen. Size is 1 inches of 1/72.
PX-corresponds to the actual pixel on the screen. This unit is not recommended because different devices can have different numbers of pixels per inch.
mm-represents millimeters, based on the physical size of the screen.
In-represents inches, which is the physical size of the screen. Each inch is equal to 2.54 centimeters, we often say the screen how many inches, refers to the length of the diagonal.

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <dimen        name="dimension_name"        >dimension</dimen></resources>

eg
Save path: Res/values/dimens.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <dimen name="Textview_height">25dp</dimen>    <dimen name="Textview_width">150dp</dimen>    <dimen name="Ball_radius">30dp</dimen>    <dimen name="Font_size">16sp</dimen></Resources>

Use in Java:

Resources res = getResources();float fontSize = res.getDimension(R.dimen.font_size);

Used in XML:

<TextView    android:layout_height="@dimen/textview_height"    android:layout_width="@dimen/textview_width"    android:textSize="@dimen/font_size"/>
Id

In the XML definition ID value, which is mapped to the R.java file, is unique, and we can use it directly as an ID in the program.

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <item        type="id"        name="id_name" /></resources>

eg
Save path: Res/values/ids.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <item type="id" name="button_ok" />    <item type="id" name="dialog_exit" /></resources>

Used in XML:

<Button android:id="@id/button_ok"    style="@style/button_style" />
Integer

Defines an integer value in XML.

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <integer        name="integer_name"        >integer</integer></resources>

eg
Save path: Res/values/integers.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <integer name="max_speed">75</integer>    <integer name="min_speed">5</integer></resources>

Use in Java:

Resources res = getResources();int maxSpeed = res.getInteger(R.integer.max_speed);
Integer-array

An integer array, corresponding to a string array.

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <integer-array        name="integer_array_name">        <item            >integer</item>    </integer-array></resources>

eg

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <integer-array name="bits">        <item>4</Item>        <item>8</Item>        <item>16</Item>        <item>32</Item>    </integer-array></Resources>

Use in Java:

Resources res = getResources();int[] bits = res.getIntArray(R.array.bits);
Typed Array

Defining type resources in XML

Grammar:

<?xml version="1.0" encoding="utf-8"?><resources>    <array        name="integer_array_name">        <item>resource</item>    </array></resources>

eg
Save Location: Res/values/arrays.xml

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <array name="Icons">        <item>@drawable/Home</Item>        <item>@drawable/settings</Item>        <item>@drawable/logout</Item>    </array>    <array name="Colors">        <item>#FFFF0000</Item>        <item>#FF00FF00</Item>        <item>#FF0000FF</Item>    </array></Resources>

Use in Java:

Resources res = getResources();TypedArray icons = res.obtainTypedArray(R.array.icons);Drawable drawable = icons.getDrawable(0);TypedArray colors = res.obtainTypedArray(R.array.colors);int color = colors.getColor(0,0);
Summarize

The above gives you a list of the values of Android in the resources, this is what we use in the actual development of the resources, how to make it reasonable for the use of their own scenarios, Android to provide us with these resources is to allow us to develop more convenient, reduce hard-coded situation, Also to standardize our development, the separation of resources and logic code will also make it easier for us to understand the design pattern of the program.

This article refers to:
Http://www.android-doc.com/guide/topics/resources/more-resources.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A014-values 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.