Android offers one more directory where you can keep files to be included in the package: /assets. It’s at the same level as /res, meaning it’s not part of the /res subdirectories. The files in /assets do not generate IDs in R.java; you must specify the file path to read them. The file path is a relative path starting at /assets.
String getStringFromAssetFile(Activity activity) {
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
String s = convertStreamToString(is);
is.close();
return s;
}
2012年11月12日 星期一
Android Resources - RAW
Android also allows raw files in addition to arbitrary XML files. These raw resources, placed in /res/raw, are raw file resources such as audio, video, or text files that require localization or references through resource IDs.
String getStringFromRawFile(Activity activity)
throws IOException {
Resources r = activity.getResources();
InputStream is = r.openRawResource(R.raw.test);
String myText = convertStreamToString(is);
is.close();
return myText;
}
String convertStreamToString(InputStream is)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = is.read();
while (i != -1) {
baos.write(i);
i = is.read();
}
return baos.toString()
}
String getStringFromRawFile(Activity activity)
throws IOException {
Resources r = activity.getResources();
InputStream is = r.openRawResource(R.raw.test);
String myText = convertStreamToString(is);
is.close();
return myText;
}
String convertStreamToString(InputStream is)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = is.read();
while (i != -1) {
baos.write(i);
i = is.read();
}
return baos.toString()
}
Android Resources - Arbitrary XML
Android allows arbitrary XML files as resources.
XML files that need to be read in this fashion are stored under the /res/xml subdirectory.
<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
XML files that need to be read in this fashion are stored under the /res/xml subdirectory.
<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
Android Resources - Color-Drawable
In Android, an image is one type of a drawable resource. Android supports another drawable resource called a color-drawable resource; it’s essentially a colored rectangle.
<resources>
<drawable name="red_rectangle">#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle">#f0f0</drawable>
</resources>
// Get a drawable
ColorDrawable redDrawable = (ColorDrawable)
activity.getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(redDrawable);
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:background="@drawable/red_rectangle"/>
To achieve the rounded corners in your Drawable, you can use the currently undocumented <shape> tag. However, this tag needs to reside in a file by itself in the /res/drawable directory.
<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>
// Get a drawable
GradientDrawable roundedRectangle = (GradientDrawable)
activity.getResources().getDrawable(R.drawable.my_rounded_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(roundedRectangle);
<resources>
<drawable name="red_rectangle">#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle">#f0f0</drawable>
</resources>
// Get a drawable
ColorDrawable redDrawable = (ColorDrawable)
activity.getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(redDrawable);
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAlign="center"
android:background="@drawable/red_rectangle"/>
To achieve the rounded corners in your Drawable, you can use the currently undocumented <shape> tag. However, this tag needs to reside in a file by itself in the /res/drawable directory.
<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>
// Get a drawable
GradientDrawable roundedRectangle = (GradientDrawable)
activity.getResources().getDrawable(R.drawable.my_rounded_rectangle);
//Set it as a background to a text view
textView.setBackgroundDrawable(roundedRectangle);
Android Resources - Dimension
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>
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>
Android Resources - Color
#RGB
#ARGB
#RRGGBB
#AARRGGBB
<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>
http://code.google.com/android/reference/android/R.color.html
#ARGB
#RRGGBB
#AARRGGBB
<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>
http://code.google.com/android/reference/android/R.color.html
Android Resources - plurals
<resources…>
<plurals name="eggs_in_a_nest">
<item quantity="one">There is 1 egg</item>
<item quantity="other">There are %d eggs</item>
</plurals>
</resources>
Resources res = my_activity.getResources();
String s1 = res.getQuantityString(R.plurals.eggs_in_a_nest, 0,0);
String s2 = res.getQuantityString(R.plurals.eggs_in_a_nest, 1,1);
String s3 = res.getQuantityString(R.plurals.eggs_in_a_nest, 2,2);
String s4 = res.getQuantityString(R.plurals.eggs_in_a_nest, 10,10);
<plurals name="eggs_in_a_nest">
<item quantity="one">There is 1 egg</item>
<item quantity="other">There are %d eggs</item>
</plurals>
</resources>
Resources res = my_activity.getResources();
String s1 = res.getQuantityString(R.plurals.eggs_in_a_nest, 0,0);
String s2 = res.getQuantityString(R.plurals.eggs_in_a_nest, 1,1);
String s3 = res.getQuantityString(R.plurals.eggs_in_a_nest, 2,2);
String s4 = res.getQuantityString(R.plurals.eggs_in_a_nest, 10,10);
訂閱:
文章 (Atom)