| Resource Type | Location | Description | XML | Java |
| Id | /res/values/any-file |
| ||
| Color | /res/values/any-file | Represents color identifiers pointing to color codes. These resource IDs are exposed in R.java as R.color.*. |
<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.*. |
| //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
|
| 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. |
| //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. |
| // 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 |
| // 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 |
| 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.*. |
| 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 意見:
張貼留言