Android 资源操作

 

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文件如下所示:


   
      Hello World from an xml sub element
   

 

4.android上的颜色.

在 strings.xml文件中申明,


#f00
#0000ff
#f0f0
#ffffff00


然后在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


#f00
#0000ff
#f0f0


 

  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,如下所示:


    
    
    
    


圆角文本框效果:

 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数组



	one
	two
	three


 

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


完.....

 


 

你可能感兴趣的:(Android智能手机开发)