android 配置文件解析

一、佈局文件:在layout目錄下,使用比較廣泛;

  我們可以爲應用定義兩套或多套佈局,例如:可以新建目錄layout_land(代表手機橫屏佈局),layout_port(代表手機豎屏佈局),系統會根據不同情況自動找到最合適的佈局文件,但是在同一界面的兩套不同佈局文件的文件名應該是相同的,只是放在了兩個不同的目錄下。

二、圖片文件:在drawable目錄下,從2.1版本以後分爲三個目錄,

  1. drawable-hdpi裏面存放高分辨率的圖片,如WVGA (480×800),FWVGA (480×854)
  2. drawable-mdpi裏面存放中等分辨率的圖片,如HVGA (320×480)
  3. drawable-ldpi裏面存放低分辨率的圖片,如QVGA (240×320)

     系統會根據機器的分辨率來分別到這幾個文件夾裏面去找對應的圖片。  

     在開發程序時爲了兼容不同平臺不同屏幕,建議各自文件夾根據需求均存放不同版本圖片。

我們可以將已經做好的圖片放到該目錄下,或者通過自定義XML文件來實現想要的圖片,例如我們可以定義shapge_1.xml放到drawable目錄下,內容如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <!--android:shape="oval"表示所要繪製的圖形是一個橢圓,默認是rectangle,長方形--> <gradient android:startColor="#0055ff88" android:centerColor="#0055ff00" android:centerY="0.75" android:endColor="#00320077" android:angle="270" /> <!--gradient 產生顏色漸變 android:angle 從哪個角度開始變 只有90的整數倍可以 --> <solid android:color="#ff4100ff"/> <!--solid表示圖形是實心的,填充裏面,#ff4100ff爲填充顏色--> <stroke android:width="2dp" android:color="#ee31ff5e" android:dashWidth="3dp" android:dashGap="2dp" /> <!-- 描邊 採用那樣的方式將外形輪廓線畫出來,width表示筆的粗細,dashWidth表示小橫線的寬度,dashGap表示小橫線之間的距離--> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp" /> <!--和CSS中的padding應該是一個道理--> <corners android:radius="6dp" /> <!--corners表示是有半徑爲5像素的圓角--> </shape>

當我們想讓一個控件根據不同狀態顯示不同圖片,可以直接在程序中控制,也可以在drawable目錄建立XML文件達到相同的效果,例如:我們可以在drawable目錄下新建文件button_back.xml

<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"android:drawable="@drawable/xxx1" /> <item android:state_pressed="true" android:drawable="@drawable/xxx2" /> <item android:state_focused="true" android:drawable="@drawable/xxx3" /> <-- 這裏還可以加N多效果和動作 只要你用的到 --> <item android:drawable="@drawable/xxx4" /> </selector>

以上XML文件可以實現一個控件(假設爲button),獲取焦點,按下按鈕,正常狀態下顯示不同圖片的效果,只需要在定義控件是引用該文件名即可,例如:

<Button android:id="@+id/Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_add_x"> </Button> <!--android:background="@drawable/button_back"指向button_back.xml文件-->

但是當我們的條件不是系統已有的事件類型,例如根據ImageView根據一個變量var的值的不同顯示不同的圖片,應該怎麼辦呢?可以在程序中寫如下代碼

1 if (條件1)
2 {
3 image.setBackground(R.id.xxx1);
4 }
5 else if (條件2)
6 {
7 image.setBackground(R.id.xxx2);
8 } ...

  或者可以用另一個簡便的方法實現相同的功能,在res/drawable下建立一個xml文件,內容如下 

<level-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:maxLevel="9" android:drawable="@drawable/battery_0" /> <item android:maxLevel="39" android:drawable="@drawable/battery_1" /> <item android:maxLevel="69" android:drawable="@drawable/battery_2" /> <item android:maxLevel="89" android:drawable="@drawable/battery_3" /> <item android:maxLevel="100" android:drawable="@drawable/battery_4" /> </level-list>

然後在layout中把imageview的src設置成已創建好的xml文件 ,程序中變換圖片時,只需要使用 imageview.getDrawable().setLevel(50);
Android會根據level的值自動選擇對應的圖片。手機顯示剩餘電量就是用這個方法來顯示不同圖片的。

三、菜單文件:在menu目錄下,寫代碼時只需在onCreateOptionsMenu方法中用MenuInflater裝載進去就OK了。格式如下,

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/enabled_item" android:title="Enabled" android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item" android:title="Disabled" android:enabled="false" android:icon="@drawable/stat_sad" /> <item android:id="@+id/enabled_item_2" android:title="Enabled" android:icon="@drawable/stat_happy" /> <item android:id="@+id/disabled_item_2" android:title="Disabled" android:enabled="false" android:icon="@drawable/stat_sad" /> </menu>

四、resource文件,在values目錄下,之所以稱之爲resource文件,是因爲values目錄下xml文件都是以resource作爲根節點,

  1.strings.xml  定義字符串的文件,格式如下: 

<resources> <string name="hello">Hello World!</string> <string name="app_name">我的應用程序</string> </resources>

  2.colors.xml  定義顏色的文件,格式如下:

<resources> <!--定義圖片顏色--> <drawable name="screen_background_black">#ff000000</drawable> <drawable name="translucent_background">#e0000000</drawable> <drawable name="transparent_background">#00000000</drawable> <!--定義文字顏色--> <color name="solid_red">#f00</color> <color name="solid_blue">#0000ff</color> <color name="solid_green">#f0f0</color> <color name="solid_yellow">#ffffff00</color> </resources>

  3.arrays.xml  定義數組的文件,格式如下:

<resources> <string-array name="planets"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> <item>Pluto</item> </string-array> <integer-array name="numbers"> <item>100</item> <item>500</item> <item>800</item> </integer-array> </resources>

  4.styles.xml  定義樣式的文件,分爲兩種用途:

   Style:以一個單位的方式用在佈局XML單個元素(控件)當中。 例如:我們可以爲TextView定義一種樣式風格,包含文本的字號大小和顏色,然後將其用在TextView特定的實例。 
   Theme:以一個單位的方式用在應用中所有的Activity當中或者應用中的某個 Activity當中。 比如,我們可以定義一個Theme,它爲window frame和panel 的前景和背景定義了一組顏色,並             爲菜單定義可文字的大小和顏色屬性,可以將這個Theme應用在你程序當中所有的Activity裏。 

<resources> <!--Theme,可以用來定義activity的主題--> <style name="Theme.Transparent"> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> <item name="android:windowBackground">@drawable/transparent_background</item> <item name="android:windowNoTitle">true</item> <item name="android:colorForeground">#fff</item> </style> <!--Style,可以用來定義某個View元素,這裏是ImageView的樣式--> <style name="ImageView120dpi"> <item name="android:src">@drawable/stylogo120dpi</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style> </resources>

個人認爲,其實不管是Theme還是Style,其實只是應用的範圍不同而已,區分的話應該是根據android:name="xxxx"的xxxx來區分吧,很明顯是不同的。

  5.dimen.xml  定義單位的文件,android中度量單位有以下幾種:

    px(象素): 屏幕實際的象素,常說的分辨率1024*768pixels,就是橫向1024px, 縱向768px,不同設備顯示效果相同。

    in(英寸): 屏幕的物理尺寸, 每英寸等於2.54釐米。 

    mm(毫米): 屏幕的物理尺寸。

    pt(點) : 屏幕的物理尺寸。1/72英寸。 

    dp/dip : 與密度無關的象素,一種基於屏幕密度的抽象單位。在每英寸160點的顯示器上,1dp = 1px。但dp和px的比例會隨着屏幕密度的變化而改變,不同設備有不同的顯示效果。 

    sp : 與刻度無關的象素,主要用於字體顯示best for textsize,作爲和文字相關大小單位。

 

<resources> <dimen name="one_pixel">1px</dimen> <dimen name="double_density">2dp</dimen> <dimen name="sixteen_sp">16sp</dimen> </resources>

 

  6.attrs.xml  定義屬性的文件,主要用在自定義的組件中,具體使用方法會在後續的如何使用自定義組件中詳細介紹,其格式如下:

 

 

<resources> <declare-styleable name="MyView"> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> </resources>

 

五、動畫文件  在anim目錄下,動畫資源分爲兩種,

 

1.實現圖片的translate、scale、rotate、alpha四種變化,還可以設置動畫的播放特性,稱爲Tween動畫。

 

<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:interpolator="@android:anim/accelerate_interpolator" android:fromXDelta="0" android:toXDelta="200" android:fromYDelta="0" android:toYDelta="180" android:duration="2000" /> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="1.0" android:toXScale="2.0" android:fromYScale="1.0" android:toYScale="2.0" android:pivotX="150%" android:pivotY="150%" android:duration="2000" /> <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="@android:integer/config_mediumAnimTime" /> <rotate ....各個屬性></rotate> <Interpolator >可以使用其子類和屬性定義動畫的運行方式,先快後慢,先慢後快等</Interpolator> </set>

 

 

2.幀動畫,逐幀播放設置的資源,稱爲Frame動畫。

 

<animation-list xmlns:android=”http://schemas.android.com/apk/res/android” android:oneshot=”true”> <item android:drawable=”@drawable/rocket_thrust1″ android:duration=”200″ /> <item android:drawable=”@drawable/rocket_thrust2″ android:duration=”200″ /> <item android:drawable=”@drawable/rocket_thrust3″ android:duration=”200″ /> </animation-list>

 

六、raw目錄下的文件,是直接複製到設備中的任意文件。它們無需編譯,添加到你的應用程序編譯產生的壓縮文件中。一般爲應用要用到的音頻或視頻文件等等

 

   要使用這些資源,可以調用Resources.openRawResource(),參數是資源的ID,即R.raw.somefilename

七、xml目錄下的文件,是程序中需要使用的普通xml文件。在運行時可以通過調用Resources.getXML()讀取。

八、assets目錄下的文件都是保持原始的文件格式,需要用AssetManager以字節流的形式讀取文件。

  1. 先在Activity裏面調用getAssets()來獲取AssetManager引用。

  2. 再用AssetManager的open(String fileName, int accessMode)方法則指定讀取的文件以及訪問模式就能得到輸入流InputStream。

  3. 然後就是用已經open file 的inputStream讀取文件,讀取完成後記得inputStream.close()。

  4.調用AssetManager.close()關閉AssetManager。        


總結:其實android中定義如此多的XML配置文件,在我看來就是爲了達到顯示層和數據層的分離,提高了可維護性,也是我們的程序代碼變得簡潔。


發佈了24 篇原創文章 · 獲贊 13 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章