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

<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);
		}
	}


完.....

 


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值