Android中的資源是指非代碼部分,片、音頻、視頻、字元等資源。一般我們都是在assets中儲存原生的檔案,如:MP3檔案,Android程式不能直接存取,必須通過AssetManager類以二進位流的形式讀取。布局類型檔案一般都放在res檔案夾下,如:anim,drawable,layout,values,xml,raw,menu等等,這些資源都可以通過R資源類直接存取。assets中的資源很少用到,res中的資源經常用到。
下面我們繼續講解如何使用這些典型布局類型檔案:
四、使用原始XML資源:如果項目中使用到了一些原始的XML檔案,那麼我們可以定義一些XML檔案供工程使用。
1)字串的定義和使用:
資源位置:res/xml/test.xml
原始XML檔案格式:
獲得XML資源方法:Resources.getXml()
引用XML資源方式:Java代碼中:R.xml.xml_name
案例: 獲得原始XML檔案的基本思路是:通過Resources.getXml()獲得XML原始檔案,得到XmlResourcePaser對象,通過該對象來判斷是文檔的開始還是結尾、是某個標籤的開始還是結尾,並通過獲得屬性的方法來遍曆XMl檔案,從而訪問MXL檔案的內容。
xml檔案:
java中核心代碼:
Resources r = getResources();
XmlResourceParser xrp = r.getXml(R.xml.person);
try{
//如果沒有到檔案尾繼續迴圈
while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT){
//如果是開始標籤
if(xrp.getEventType() == XmlResourceParser.START_TAG){
String name = xrp.getName();
//判斷標籤名是否等於person
if(name.equals("person")){
counter++;
//獲得標籤屬性追加到StringBuilder中
sb.append("第"+counter+"條使用者資訊:\n");
sb.append("name:"+xrp.getAttributeValue(0)+"\n");
sb.append("age:"+xrp.getAttributeValue(1)+"\n");
sb.append("gender:"+xrp.getAttributeValue(2)+"\n\n");
}
}else if(xrp.getEventType() == XmlPullParser.END_TAG){
}else if(xrp.getEventType() == XmlPullParser.TEXT){
}
xrp.next();
}
//將StringBuilder設定為TextView的文本
myTextView.setText(sb.toString());
myButton.setText("退出");
}catch(XmlPullParserException e){
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
五、使用drawables資源:一些圖片或者顏色資源,主要用來繪製螢幕,分為三類:Bitmap File(位元影像檔案)、Color Drawable (顏色)、NIne-Patch Image(九片圖片)。
1)drawable資源的定義和使用:
資源位置:res/drawable/file_name.jpg/file_name.png/file_name.gif
獲得字串方法:Resources.getDrawable()
引用資源方式:Java代碼中:R.drawable.file_name
字串XML檔案中:@[package:]drawable/file_name
案例:myImageView = (ImageView)findViewById(R.id.mypic);
Resources r = getResources();
Drawable d = r.getDrawable(R.drawable.moto);
myImageView.setImageDrawable(d);
六、使用布局(layout)資源:布局資源是Android中最常用的一種資源,Android可以將螢幕中組件的布局方式定義在一個XML檔案中,這有點像Web開發中的HTML頁面,通過Activity.setContentView()方法將布局檔案展示在Activity上。Android通過LayoutInflater類將XML檔案中的組件解析為可視化的視圖組件。
1)布局檔案的定義和使用:
資源位置:res/layout/my_layout.xml(任意名稱)
布局XML檔案格式:
<布局類 xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/stirng_name" (屬性)>
<視圖組件或者其他嵌套布局類>
獲得XML資源的方法:Activity.setContentView()
引用資源方式:Java代碼中:R.layout.my_layout
字串XML檔案中:@[package:]layout/my_layout
案例:布局檔案main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/dimen_text"
android:layout_width="fill_parent"
android:layout_height="@dimen/text_height"
android:textColor="@color/white"
android:background="@color/blue"
android:text="@string/hello_world" />
java核心代碼:setContentView(R.layout.main);
出處:http://blog.csdn.net/cl05300629/article/details/17716895 作者:佇望碧落