1. 程式人生 > >Android資原始檔中各種XML的作用與解釋

Android資原始檔中各種XML的作用與解釋

眾所周知,XML是一種可擴充套件標記語言,它被用來傳輸和儲存資料。在Android中也會隨處可見XML檔案,包括一個android專案不可缺少的AndroidManifest.xml清單檔案,res資原始檔目錄下的anim/drawable/layout/menu/values中等,目錄截圖如下。其中清單檔案中內容最多最複雜,完全可以在其他文章中再來講解,所以本文主要講解res目錄下的XML的作用與內容。


一、anim目錄

anim目錄下的xml主要是用於android中的動畫,包括Frame animation(逐幀動畫)與Tween animation(補間動畫 )。

1.逐幀動畫

逐幀動畫是一系列圖片按照一定的順序展示的過程,和放電影的機制很相似。可以理解成GIF,一幀一幀的顯示圖片。
程式碼:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
  <item android:drawable="@drawable/a_01" android:duration="50"/>
  <item android:drawable="@drawable/a_02" android:duration="50"/>
  <item android:drawable="@drawable/a_03" android:duration="50"/>
  <item android:drawable="@drawable/a_04" android:duration="50"/>
  <item android:drawable="@drawable/a_05" android:duration="50"/>
  <item android:drawable="@drawable/a_06" android:duration="50"/>
</animation-list>
<animation-list>元素是必須的,並且必須要作為根元素,可以包含一或多個元素;android:onshot如果定義為true的話,此動畫只會執行一次,如果為false則一直迴圈;元素代表一幀動畫, android:drawable指定此幀動畫所對應的圖片資源;android:druation代表此幀持續的時間, 整數,單位為毫秒。

2. 補間動畫

補間動畫包括旋轉、 平移、縮放和透明度等效果。
程式碼: ① 旋轉
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
  <!--fromDegrees:開始的角度
  toDegrees: 結束的角度, +表示是正的 
  pivotX: 用於設定旋轉時的x軸座標 例 當值為"50",表示使用絕對位置定位 當值為"50%",表示使用相對於控制元件本身定位 當值為"50%p",表示使用相對於控制元件的父控制元件定位 
  pivotY: 用於設定旋轉時的y軸座標 --> 
  <rotate 
    android:fromDegrees="0" 
    android:toDegrees="+360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="1000"/>
</set>


② 平移
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
    <!--
    始x軸座標
    止x軸座標
    始y軸座標
    止y軸座標縮放
    -->
    <translate
      android:fromXDelta="0%"
      android:toXDelta="100%"
      android:fromYDelta="0%"
      android:toYDelta="100%"
      android:duration="2000"/>
</set>
③ 縮放
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
    <!--
    起始x軸座標
    止x軸座標
    始y軸座標
    止y軸座標
    x軸的座標
    y軸的座標
    -->
    <scale
      android:fromXScale="1.0"
      android:toXScale="0.0"
      android:fromYScale="1.0"
      android:toYScale="0.0"
      android:pivotX="50%"
      android:pivotY="50%"
      android:duration="1000"/>
</set>
④ 透明度
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
    <!-- fromAlpha和toAlpha是起始透明度和結束時透明度 -->
    <alpha
      android:fromAlpha="1.0"
      android:toAlpha="0.0"
      android:startOffset="500"
      android:duration="500"/>
</set>

二、drawable目錄

drawable目錄主要是為了定義圖片、按鈕的背景及其點選狀態。主要使用shape標籤和selector標籤。

1.shape標籤

shape主要是定義一個形狀,然後可以設定給某個按鈕作為背景,最常用的就是圓角按鈕。
程式碼:
<?xml version="1.0" encoding="utf-8"?>  
<shape xmlns:android="http://schemas.android.com/apk/res/android"      
      android:shape=["rectangle"|"oval"|"line"|"ring"] > 

     <!-- 圓角 -->     
     <corners          
            android:radius="integer"          
            android:topLeftRadius="integer"          
            android:topRightRadius="integer"          
            android:bottomLeftRadius="integer"          
            android:bottomRightRadius="integer" />  
      <!-- 漸變 -->      
      <gradient          
            android:angle="integer"          
            android:centerX="integer"          
            android:centerY="integer"          
            android:centerColor="integer"          
            android:endColor="color"          
            android:gradientRadius="integer"          
            android:startColor="color"          
            android:type=["linear"|"radial"|"sweep"]          
            android:useLevel=["true"|"false"] />      
      <padding          
            android:left="integer"          
            android:top="integer"          
            android:right="integer"          
            android:bottom="integer" />      
      <size          
            android:width="integer"          
            android:height="integer" />      
      <solid          
            android:color="color" />  
      <!-- 描邊 -->   
      <stroke          
            android:width="integer"          
            android:color="color"          
            android:dashWidth="integer"          
            android:dashGap="integer" />  
</shape>

2.selector標籤

selector主要是定義不同狀態按鈕的背景等。
程式碼:
<?xml version="1.0" encoding="utf-8" ?>   
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- 預設時的背景圖片-->  
    <item android:drawable="@drawable/a_01" />    
    <!-- 沒有焦點時的背景圖片 -->  
    <item android:state_window_focused="false"   
          android:drawable="@drawable/a_01" /> 
    <!-- 非觸控模式下獲得焦點並單擊時的背景圖片 -->  
    <item android:state_focused="true" 
          android:state_pressed="true"   
          android:drawable="@drawable/a_02" /> 
    <!-- 觸控模式下單擊時的背景圖片-->  
    <item android:state_focused="false" 
          android:state_pressed="true"        
          android:drawable="@drawable/a_03" />  
    <!--選中時的圖片背景-->  
    <item android:state_selected="true"   
          android:drawable="@drawable/a_04" />   
    <!--獲得焦點時的圖片背景-->  
    <item android:state_focused="true"  
          android:drawable="@drawable/a_05" />  
</selector>

三、layout目錄

layout目錄主要存放android的佈局檔案,包括android中的五大布局:LinearLayout(線性佈局)、FrameLayout(幀佈局)、RelativeLayout(相對佈局)、AbsoluteLayout(絕對佈局)和TableLayout(表格佈局)。這裡就不在做詳細講解,相信大家在使用時也沒有太大問題。

四、menu目錄

menu目錄主要用來存放選單的樣式,包括點選手機底部的選單鍵和頂部actionbar中設定的選單按鈕時的彈出框的選單項。 程式碼:
<?xml version="1.0" encoding="utf-8"?>  
<menu xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:id="@+id/connect"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_send"  
          android:title="連線" />  
    <item android:id="@+id/disconnect"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_close_clear_cancel"  
          android:title="斷開" />  
    <item android:id="@+id/search"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_search"  
          android:title="發現" />  
    <item android:id="@+id/view"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_view"  
          android:title="檢視" />  
    <item android:id="@+id/help"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_help"  
          android:title="幫助" /> 
    <item android:id="@+id/exit"  
          android:orderInCategory="100"  
          android:showAsAction="never"  
          android:icon="@android:drawable/ic_menu_revert"  
          android:title="退出" />  
</menu>  
效果:

五、values目錄

values目錄下的東西比較多,包括arrays.xml/colors.xml/dimens.xml/ids.xml/strings.xml/styles.xml,如下圖所示:

1.arrays.xml

arrays.xml檔案中用於放各種陣列資料,比如字串陣列、整型陣列等,陣列中的資料可能是具體的值,也有可能是對資源資料的引用。
程式碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="select_items">
	    <item>one</item>
	    <item>two</item>
	    <item>three</item>
	    <item>four</item>
	</string-array>
</resources>
使用:
String[] items = getResources().getStringArray(R.array.select_items);
items陣列中的資料就是arrays.xml檔案中對應資源id R.array.selec_items中的資料。

2.colors.xml

colors.xml檔案中主要用來說明需要的顏色值,也可以在res目錄下另外新建一color資料夾用來存放這些xml檔案 程式碼:
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
     <color name="red">#ff00000</color>  
     <color name="black">#000000</color>
     <color name="white">#ffffff</color>  
</resources>
使用:
btn.setBackgroundColor(getResources().getColor(R.color.red));

3.dimens.xml

dimens.xml用來定義控制元件的尺寸和文字的大小,在其中定義是為了方便做螢幕適配。 程式碼:
 <resources>
    <!-- 控制元件的大小 -->
    <dimen name="title_width">200dp</dimen>
    <dimen name="title_height">50dp</dimen>
    
    <!-- 字型的大小 -->
    <dimen name="info_size">20sp</dimen>
    <dimen name="tip_size">16sp</dimen>
</resources>
使用:
<TextView
        android:layout_width="@dimen/title_width"
        android:layout_height="@dimen/title_height"
        android:textSize="@dimen/info_size"/>

4.ids.xml

ids.xml為應用的相關資源提供唯一的資源id。 程式碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item name="send" type="id"/>
    <item name="public" type="id"/>
</resources>
使用:
<TextView
        android:id="@id/send"
        android:layout_width="@dimen/title_width"
        android:layout_height="@dimen/title_height"
        android:textSize="@dimen/info_size"/>

5.strings.xml

Android建議將在螢幕上顯示的文字定義在strings.xml中,而且這樣做也可以做到國際化。
程式碼:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">TestDemo</string>
    <string name="action_add">新增</string>
    <string name="action_del">刪除</string>
    <string name="action_settings">設定</string>
    <string name="action_about">關於</string>
    <string name="action_suggest">建議反饋</string>
</resources>
使用:
<TextView
        android:id="@id/send"
        android:layout_width="@dimen/title_width"
        android:layout_height="@dimen/title_height"
        android:textSize="@dimen/info_size"
        android:text="@string/action_add"/>

6.styles.xml

styles.xml主要用來存放android的主題與樣式 程式碼:
<resources>
    <style name="myDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/transparent</item>
        <!-- 設定dialog背景 -->
        <item name="android:windowNoTitle">true</item>
        <!-- 無標題 -->
        <item name="android:windowIsFloating">true</item>
    </style>
</resources>