NO IMAGE
 

1.獲取裝置中的文字檔案,該檔案在raw目錄下面。

 
  String getStringFromAssetFile(Context activity)  throws IOException
{
AssetManager am = activity.getAssets();
InputStream is = am.open("test.txt");
String s = convertStreamToString(is);
is.close();
return s;
}

2.同樣獲取資源的方式,如下所示:

public void testRawFile()
{
try
{
String s = getStringFromRawFile(this.mContext);
this.reportString(s);
}
catch(Throwable t)
{
this.reportString("error:"   t.getMessage());
}
}
private String getStringFromRawFile(Context activity)
throws IOException
{
Resources r = activity.getResources();
InputStream is = r.openRawResource(R.raw.test);
String myText = convertStreamToString(is);
is.close();
return myText;
}
private 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();
}

這樣方式同方式一差不多,讀取的都是同一個檔案。

 

3.讀取XML檔案,如下所示:

public void testXML()
{
try
{
String x = getEventsFromAnXMLFile(this.mContext);
reportString(x); 
}
catch(Throwable t)
{
reportString("error reading xml file:"   t.getMessage());
}
}
private String getEventsFromAnXMLFile(Context 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檔案如下所示:

<rootelem1>
<subelem1>
Hello World from an xml sub element
</subelem1>
</rootelem1>

 

4.android上的顏色.

在 strings.xml檔案中申明,

<!-- To support colors -->
<color name="red">#f00</color>
<color name="blue">#0000ff</color>
<color name="green">#f0f0</color>
<color name="main_back_ground_color">#ffffff00</color>

然後在JAVA中使用,如下

public void testColor()
{
Resources res = this.mContext.getResources();
int mainBackGroundColor 
=  res.getColor(R.color.main_back_ground_color);
reportString("mainBackGroundColor:"   mainBackGroundColor);
}

5.測試圖片

 private void testImage()
{
//Call getDrawable to get the image
Drawable d = getResources().getDrawable(R.drawable.sample_image);
//You can use the drawable then to set the background
this.getTextView().setBackgroundDrawable(d);  	
//or you can set the background directly from the Resource Id
this.getTextView().setBackgroundResource(R.drawable.sample_image);
}

6.測試Drawable

<!-- Color Drawables -->
<drawable name="red_rectangle">#f00</drawable>
<drawable name="blue_rectangle">#0000ff</drawable>
<drawable name="green_rectangle">#f0f0</drawable>

 

  private void testColorDrawables()
{
// Get a drawable
ColorDrawable redDrawable = 
(ColorDrawable)
getResources().getDrawable(R.drawable.red_rectangle);
//Set it as a background to a text view
this.getTextView().setBackgroundDrawable(redDrawable);
}

7.測試Shape

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>

圓角文字框效果:

 private void testShape()
{
// Get a drawable
GradientDrawable roundedRectangle = 
(GradientDrawable)
getResources().getDrawable(R.drawable.my_rounded_rectangle);
//Set it as a background to a text view
this.getTextView().setBackgroundDrawable(roundedRectangle);
}

 

8. 測試Array陣列

<!-- To support arrays -->
<string-array name="test_array">
<item>one</item>
<item>two</item>
<item>three</item>
</string-array>

 

private void reportArray(int arrayId)
{
Resources res = this.mContext.getResources();
String strings[] = res.getStringArray(arrayId);
for (String s: strings)
{
this.mReportTo.reportBack(tag, s);
}
}

完…..