Resource Type Location Description XML Java
Id /res/values/any-file  
<resources>
<item type="id" name="text"/>
</resources>

 
Color /res/values/any-file Represents color identifiers pointing to color codes. These
resource IDs are exposed in R.java as R.color.*.
<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>
<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textColor="@color/ red"
          android:text="Sample Text to Show Red Color"/>
int mainBackGroundColor
= activity.getResources.getColor(R.color.main_back_ground_color);
String /res/values/any-file Represents string resources. String resources
allow
Java-formatted strings and raw HTML in addition
to simple
strings. These resource IDs are exposed in
R.java as R.string.*.
<resources>
    <string name="simple_string">simple string</string>
    <string name="quoted_string">"quoted'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>
 
 
<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAlign="center"
          android:text="@string/tagged_string"/>
//Read a simple string and set it in a text view
String 
      simpleString = 
      activity.getString(R.string.simple_string);
textView.setText(simpleString);
//Read 
      a quoted string and set it in a text view
String quotedString = 
      activity.getString(R.string.quoted_string);
textView.setText(quotedString);
//Read 
      a double quoted string and set it in a text view
String 
      doubleQuotedString = 
      activity.getString(R.string.double_quoted_string);
textView.setText(doubleQuotedString);
//Read 
      a Java format string
String javaFormatString = 
      activity.getString(R.string.java_format_string);
//Convert the 
      formatted string by passing in arguments
String substitutedString = 
      String.format(javaFormatString, "Hello" , "Android");
//set the output 
      in a text view
textView.setText(substitutedString);
//Read an html 
      string from the resource and set it in a text view
String 
      htmlTaggedString = 
      activity.getString(R.string.tagged_string);
//Convert it to a text span 
      so that it can be set in a text view
//android.text.Html class allows 
      painting of "html" strings
//This is strictly an Android class and does 
      not support all html tags
Spanned textSpan = 
      android.text.Html.fromHtml(htmlTaggedString);
//Set it in a text 
      view
textView.setText(textSpan);
Dimension /res/values/any-file

Represents dimensions or sizes of various elements
or views in
Android. Supports pixels, inches, mil-
limeters, density-independent
pixels, and scale-
independent pixels. These resource IDs are
exposed
in R.java as R.dimen.*.

  • px: Pixels
  • in: Inches
  • mm: Millimeters
  • pt: Points
  • dp: Density-independent pixels based on a 160-dpi (pixel density per
    inch) screen
    (dimensions adjust to screen density)
  • sp: Scale-independent pixels (dimensions that allow for user sizing;
    helpful for use in
    fonts)


<resources>
    <dimen name="mysize_in_pixels">1px</dimen>
    <dimen name="mysize_in_dp">5dp</dimen>
    <dimen name="medium_size">100sp</dimen>
</resources>
 
 
<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textSize="@dimen/medium_size"/>
float dimen = 
      activity.getResources().getDimension(R.dimen.mysize_in_pixels);
Image /res/drawable/multiple-
files
Represents image resources. Supported images
include
.jpg, .gif, and .png. Each image is in a
separate file and gets its own
ID based on the file
name. These resource IDs are exposed in R.java
as
R.drawable.*. The image support also includes an
image type
called a stretchable image that allows
portions of an image to stretch
while other portions
of that image stay static.
<Button
      android:id="@+id/button1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Dial"
      android:background="@drawable/sample_image"
/>
//Call getDrawable to get the image
BitmapDrawable d = 
      activity.getResources().getDrawable(R.drawable.sample_image);
//You can 
      use the drawable then to set the 
      background
button.setBackgroundDrawable(d);
//or you can set the 
      background directly from the Resource 
      Id
button.setBackgroundResource(R.drawable.icon);
Color
Drawable
/res/values/any-file Represents rectangle of colors to be used as
view
backgrounds or general drawables like bitmaps.
You can use this
instead of specifying a single
colored bitmap as a background. In Java,
this is
equivalent to creating a colored rectangle and set-
ting it
as a background for a view.
The <drawable> value tag in the
values subdirec-
tory supports this. These resource IDs are
exposed
in R.java as R.drawable.*.
Android also supports rounded
rectangles and
gradient rectangles through XML files placed
in
/res/drawable with the root XML tag <shape>.
These resource
IDs are also exposed in R.java as
R.drawable.*. Each file name in this
case translates
to a unique drawable ID.
<resources>
    <drawable name="red_rectangle">#f00</drawable>
    <drawable name="blue_rectangle">#0000ff</drawable>
    <drawable name="green_rectangle">#f0f0</drawable>
</resources>

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:textAlign="center"
          android:background="@drawable/red_rectangle"/>
// Get a drawable
ColorDrawble redDrawable 
      =
(ColorDrawable)
activity.getResources().getDrawable(R.drawable.red_rectnagle);
//Set 
      it as a background to a text 
    view
textView.setBackground(redDrawable);
Shape /res/drawable/multiple-
files
 
<shape>
    <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>
// Get a drawable
GradientDrawable roundedRectangle 
      =
(GradientDrawable)
activity.getResources().getDrawable(R.drawable.red_rectnagle);
//Set 
      it as a background to a text 
      view
textView.setBackground(roundedRectangle);
Layout /res/layout/any-file  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
 <Button    android:id="@+id/b1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+string/hello"
   />  
</LinearLayout>
public void onCreate(Bundle 
      savedInstanceState)
    
      {
        
      super.onCreate(savedInstanceState);
        
      setContentView(R.layout.main);
        
      TextView tv = 
      (TextView)this.findViewById(R.id.text1);
        
      tv.setText("Try this text instead");
    }
Arbitrary
XML Files
/res/xml/*.xml Android allows arbitrary XML files as resources.
These
files will be compiled by the AAPT com-
piler. These resource IDs are
exposed in R.java as
R.xml.*.
<rootelem1>
   <subelem1>
      Hello World from an xml sub element
   </subelem1>
</rootelem1>
Resources res = 
      activity.getResources();
XmlResourceParser xpp = 
      res.getXml(R.xml.test);
 
private String 
      getEventsFromAnXMLFile(Activity activity)
throws 
      XmlPullParserException, IOException
{
   StringBuffer sb = 
      new StringBuffer();
   Resources res = 
      activity.getResources();
   XmlResourceParser xpp = 
      res.getXml(R.xml.test);
  
   
      xpp.next();
   int eventType = 
      xpp.getEventType();
    while (eventType != 
      XmlPullParser.END_DOCUMENT)
    
      {
        if(eventType == 
      XmlPullParser.START_DOCUMENT)
        
      {
           
      sb.append("******Start 
      document");
        
      }
        else if(eventType == 
      XmlPullParser.START_TAG)
        
      {
           
      sb.append("\nStart tag 
      "+xpp.getName());
        
      }
        else if(eventType == 
      XmlPullParser.END_TAG)
        
      {
           
      sb.append("\nEnd tag 
      "+xpp.getName());
        
      }
        else if(eventType == 
      XmlPullParser.TEXT)
        
      {
           
      sb.append("\nText 
      "+xpp.getText());
        
      }
        eventType = 
      xpp.next();
    }//eof-while
    
      sb.append("\n******End document");
    return 
      sb.toString();
}//eof-function
Arbitrary Raw
Resources
/res/raw/*.* Android allows arbitrary noncompiled binary or
text
files under this directory. Each file gets a unique
resource
ID. These resource IDs are exposed in
R.java as R.raw.*.
String getStringFromRawFile(Activity 
      activity)
{
    Resources r = 
      activity.getResources();
    InputStream is = 
      r.openRawResource(R.raw.test);
    String myText = 
      convertStreamToString(is);
    
      is.close();
    return myText;
}
String 
      convertStreamToString(InputStream is)
{  
    
      ByteArrayOutputStream baos = new 
      ByteArrayOutputStream();
    int i = 
      is.read();
    while (i != -1)
    
      {
        
      baos.write(i);
        i = 
      baos.read();
    }
    return 
      baos.toString();
}
Arbitrary Raw
Assets
/assets/*.*/*.* Android allows arbitrary files in arbitrary
subdirec-
tories, starting at the /assets subdirectory. These
are
not really resources, but raw files. This directo-
ry, unlike the /res
subdirectory, allows an arbitrary
depth of subdirectories. These files
do not generate
any resource IDs. You have to use a relative
path
name starting at and excluding /assets.

 
//Note: Exceptions are not shown in the code
String 
      getStringFromAssetFile(Activity activity)
{
    
      AssetManager am = activity.getAssets();
    InputStream 
      is = am.open("test.txt");
    String s = 
      convertStreamToString(is);
    
      is.close();
    return s;
}

0 意見: