Definition and use of key Android Resources

Source: Internet
Author: User
Tags java format
1. String Array
Definition:
<resources...><string-array name = "test_array"><item>one</item><item>two</item><item>three</item></string-array></resources>

Call:

Resources res = youractivity. getresources (); // youractivity is your activitystring s [] = res. getstringarray (R. array. test_array );

2. Plural resource definition:

<resources...><plurals name = "eggs_in_a_nest_text"><item quantity="one">There is 1 egg</item><item quantity="other">There is %d eggs</item><item>three</item></plurals></resources>
Call:
Resources res = youractivity. getresources (); // youractivity is your activitystring S1 = res. getquantitystring (R. plurals. eggs_in_a_nest_text, 0, 0); // The first parameter of getquantitystring () is the plural ID, // The second parameter is the string to be used. If it is 1, the string is used as is, if it is not set to 1, the third parameter must be provided. Its value is placed at the position of % d, string S1 = res. getquantitystring (R. plurals. eggs_in_a_nest_text, 1, 1 );
3. More information about string resources definition string resource XML syntax
<resources><string name="simple_string">simple string</string><string name="quoted_string">"quoted 'xyz' string"</string><string name="double_quoted_string">\"double quotes\"</string><string name="java_format_string">hello %2$s Java format string. %1$s again</string><string name="tagged_string">Hello <b><i>Slanted Android</i></b>, You are bold.</string></resources>
Use in Java code:
String simpleString = activity.getString(R.string.simple_string);textView.setText(simpleString);
Use in XML:
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:text="@string/tagged_string"/>
4. Color resources are defined in XML:
<resources><color name="red">#f00</color><color name="blue"#0000ff</color><color name="green">#f0f0</color><color name="main_back_ground_color">#ffffff00</color></resources>
Use in Java:
int mainBackGroundColor = activity.getResources.getColor(R.color.main_back_ground_color);
Use in XML:
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textColor="@color/red"android:text="Sample Text to Show Red Color"/>
5. dimension resources are defined in XML:
<resources><dimen name="mysize_in_pixels">1px</dimen><dimen name="mysize_in_dp">5dp</dimen><dimen name="medium_size">100sp</dimen></resources>
Px: pixel
In: inches
MM: mm
PT: LB
DP: density independent pixels, based on 160 DPI (no inches of pixels) screen (size adapted to screen density)
SP: pixels unrelated to the proportion (this size supports user adjustment and is suitable for font use) used in Java code:
float dimen = activity.getResources().getDimension(R.dimen.mysize_in_pixels);
Use in XML:
<TextView android:layout_width="fill_parent"android:layout_height="wrap_content"android:textSize="@dimen/medium_size"/>
6. image resources android will automatically generate IDS for image resources in the/RES/drawable sub-directories. jpg, PNG, and GIF images are supported. Like sample. jgp, R. drawable. sample when used
Use in Java:
BitmapDrawble d = activity.getResources().getDrawable(R.drawable.sample_image);button.setBackgroundDrawable(d);button.setBackgroundResource(R.drawable.sample);
7. color map resources in Android, images are graphical object Resources. Android supports another type of graphic object resource called a "color chart", which is actually a color rectangle.
To define a color chart resource, it is in/RES/values/drawable. xml
<resources><drawable name="red_rectangle">#f00</drawable></resources>
Use in Java:
ColorDrawable redDrawable = (ColorDrawable)activity.getResources().getDrawable(R.drawable.red_rectangle);textView.setBackgroundDrawable(redDrawable);

Defines the rounded rectangle:
In/RES/drawable/my_rounded_rectangle.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#f0600000"/><stroke android:width="3dp" color="#ffff8080"/><corners android:radius="13dp"/><padding android:left="10dp" android:top="10dp"<android:right="10dp" android:bottom="10dp"/></shape>
Use in Java:
GradientDrawable roundedRectangle = (GradientDrawable)activity.getResources().getDrawable(R.drawable.my_rounded_rectangle);textView.setBackgroundDrawable(roundedRectangle);
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.