跟我學Android之七 資源文件

語言: CN / TW / HK

視頻課:http://edu.csdn.net/course/play/7621

本章內容

第1節  字符串、顏色和尺寸資源
第2節  數組資源
第3節  Drawable資源
第4節  佈局資源
第5節  樣式和主題資源




本章目標 



熟練掌握字符串、顏色和尺寸資源的使用。
熟練掌握數組資源的用法。
掌握各種Drawable資源的用法。
掌握佈局資源的用法
掌握樣式和主題資源的使用。




字符串常量資源 



使用字符串常量資源,當有字符串常量需要在程序中使用時應該定義字符串常量資源,在res/values/string.xml中定義。


<?xml version="1.0" encoding="utf-8"?><resources><string name=“okLabel”>確定</string></resources>
在XML佈局中使用






<Button … android:text=“@string/okLabel” />
在java代碼中使用 
Button okBtn = (Button)findViewById(R.id.okBtn);okBtn.setText(getString(R.string.okLabel));

顏色值通過RGB(紅、綠、藍)三原色和一個透明度(Alpha)值表示。它必須以“#”開頭,後面接Alpha-Red-Green-Blue形式的內容。其中,Alpha值可以省略,如果省略,表示顏色默認是完全不透明的#RGB:使用紅、綠、藍三原色的值來表示顏色,其中,紅、綠和藍採用0~f來表示。例如,要表示紅色,可以使用#f00。#ARGB:使用透明度以及紅、綠、藍三原色來表示顏色,其中,透明度、紅、綠和藍均採用0~f來表示。例如,要表示半透明的紅色,可以使用#6f00。#RRGGBB:使用紅、綠、藍三原色的值來表示顏色,與#RGB不同的是,這裏的紅、綠和藍使用00—ff來表示。例如,要表示藍色,可以使用#0000ff。#AARRGGBB:使用透明度以及紅、綠、藍三原色來表示顏色,其中,透明度、紅、綠均採用00-ff來表示。例如,要表示半透明的綠色,可以使用#6600ff00。



顏色常量資源的使用


顏色的定義是通過RGB三色和一個alpha值來定義的,#RGB、#ARGB、#RRGGBB、#AARRGGBB,在資源文件中定義顏色,一般在res/values下建議colors.xml文件,定義顏色如下:


<resources>
        <color name=“red”>#ff0000</color>
</resources>

在代碼中使用顏色



int color = Resources.getSystem().getColor(R.color.red);btn.setBackgroundColor(color);

在佈局文件中使用顏色




android:background="@color/red"



度量單位

屬性中的度量單位
px(像素)不同設備的顯示效果相同
in(英寸)長度單位
mm(毫米)長度單位
pt(磅)1/72英寸
dp(與密度無關的像素)
一種基於屏幕密度的抽象單位
在每英寸160個點的顯示器上,1dp=1px
dip(與dp相同)
sp(與刻度無關的像素)
與dp類似,但是可以根據用户字體大小縮放
建議用sp用作字體大小的單位











使用尺寸常量資源,在尺寸常量資源中定義一些固定不變的尺寸信息,如:間隔,在res/values/dimens.xml中定義。



<resources><!-- Default screen margins, per the Android Design guidelines. --><dimen name="horizontal_margin">16dp</dimen><dimen name="vertical_margin">16dp</dimen></resources>
在XML佈局中使用 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:paddingBottom="@dimen/vertical_margin"android:paddingLeft="@dimen/horizontal_margin“ …>
在代碼中使用 
int margin = getResources().getDimension(R.dimen. horizontal_margin);



數組資源 




文件位於res\values目錄下,根元素是<resources></resources>標記,在該元素中,包括以下3個子元素
<array>子元素:用於定義普通類型的數組。
<integer-array>子元素:用於定義整數數組。
<string-array>子元素:用於定義字符串數組



使用數組常量資源,當有固定內容的數組需要在程序中使用時可以定義數組常量資源,在res/values/arrays.xml中定義。


<?xml version="1.0" encoding="utf-8"?><resources><string-array name="booktype"><item>語言類</item><item>工具類</item></string-array></resources>


在代碼中使用,可以使用ID的地方直接使用ID就可以,在不方便使用ID的地方可以通過方法調用使用。



String[] array = getResources().getStringArray(R.array.booktype);



.9.png圖片在android 環境下具有自適應調節大小的能力

shape可以用於定義各種UI的元素屬性,通常在res/drawable下建立相應的XML文件用於定義shape,solid用於定義實心填充,比如:

<solid android:color="#ff9d77"/>

gradient用於定義漸變色,比如:


<gradient  android:startColor="#eb7720"  android:endColor="#ddbda5" android:angle="270” />


stroke用於定義描邊 



<strokeandroid:width="2dp"android:color="#dcdcdc" />





佈局資源文件 

佈局資源文件放置在res\layout目錄下,佈局資源文件的根元素通常是各種布管理器,在該佈局管理器中,通常是各種View組件或是嵌套的其他佈局管理器。
加載佈局  

setContentView(R.layout.main);

引入佈局





<include layout=“@layout/image”/>



樣式與主題概述 

Android中的視圖可以通過樣式和主題進行外觀的定製,通常在res/values下建立styles.xml來定義樣式和主題,主題和樣式都在使用style標籤進行定義,通過name屬性確定一個唯一的名字,通過parent屬性指定用於繼承的父類樣式,通過子標籤item定義各種樣式的內容,示例如下:

使用樣式來改變單個視圖外觀,比如:TextView

<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="mystyle"><item name="android:textColor">#FF0000</item><item name="android:textSize">20sp</item><item name="android:background">@drawable/shap_background</item></style></resources>


首先在res/values下建立styles.xml來定義樣式
<resources> <style name="textViewStyle” parent="textViewStyle"> <item name="android:textColor">#FF0000</item> <item name="android:textSize">20sp</item> </style><style name="testTheme"> <item name="android:windowNoTitle">true</item>   <item name="android:windowFullscreen">?android:windowNoTitle</item>   </style> </resources><strong></strong>

在TextView中使用樣式 



<TextView android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/hello_world”  style="@style/mystyle” />

使用主題來改變整個應用的外觀,當把樣式用來改變應用或者Activity的時候就叫做主題了,首先在res/values下建立styles.xml來定義樣式 



<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="mytheme"><item name="android:windowNoTitle">true</item><item name="android:windowFullscreen">?android:windowNoTitle</item></style></resources>

在Application中使用主題 



<applicationandroid:allowBackup="true“ android:icon="@drawable/ic_launcher"android:label="@string/app_name”  android:theme="@style/mytheme" >