15 资源管理详解02
Android中的资源介绍:(res和assets)
使用原始的xml数据:
如果项目中使用到了一些原始的xml文件,那么,我们可以定义一些xml文件供工程使用,xml文件定义在工程的res\xml\目录下:
小案例:
代码:
res->xml->users.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <user uname="xiao xiong" phone="111111111"></user> <user uname="da xiong" phone="222222222"></user> </resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:onClick="test"/> </LinearLayout>
Test_xmlActivity.java:
package test.xml; import java.io.IOException; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.content.res.XmlResourceParser; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Test_xmlActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void test(View view) throws XmlPullParserException, IOException{ String text =""; //以下对象是对xml文件的解析对象 XmlResourceParser xrp = this.getResources().getXml(R.xml.users); //sax解析步骤(基于事件的解析方式) //会挨个解析每一个节点 while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){ //如果解析没有到结尾,就分部往下走 if(xrp.getEventType()==XmlResourceParser.START_TAG){ String tagname = xrp.getName(); if(tagname.equals("user")){ //获取信息,某一个节点的属性值 String uname = xrp.getAttributeValue(0); String phone = xrp.getAttributeValue(1); text+="姓名:" +uname+"电话"+phone+";\n";//把两个内容加到text中 } } xrp.next();//解析下一个 } TextView textView=(TextView)findViewById(R.id.textView1); textView.setText(text); } }
运行结果:
使用drawables资源:
drawable资源是一些图片资源,主要用来绘制屏幕,通过Resources.getDrawable()方法获得。
drawable资源分为三类:Bitmap File(位图文件),Color Drawable(颜色),Nine-Patch Image(九片图片)。这里讲述常用的位图文件的使用。
Android中支持的位图文件有png,jpg和gif。
代码:
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" <!-- android:background="@drawable/a3" 也可以这样的方式使用--> > <TextView android:layout_width="fill_parent" android:layout_height="248dp" android:text="@string/hello" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button1" android:onClick="test1"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button2" android:onClick="test2"/> </LinearLayout>
Test_drawableActivity.java:
package test.drawable; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.View; public class Test_drawableActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void test1(View view){ Drawable drawable=this.getResources().getDrawable(R.drawable.a1); this.getWindow().setBackgroundDrawable(drawable); } public void test2(View view){ Drawable drawable=this.getResources().getDrawable(R.drawable.a2); this.getWindow().setBackgroundDrawable(drawable); } }
运行结果:
使用布局(layout)资源:(讲到UI的时候细讲)
使用菜单(menu)资源: