1. 程式人生 > >Android專案中各個資料夾的含義和用途詳解

Android專案中各個資料夾的含義和用途詳解

1.src:存放所有的*.java源程式。

2.gen:為ADT外掛自動生成的程式碼檔案儲存路徑,裡面的R.java將儲存所有的資源ID

3.assets:可以存放專案一些較大的資原始檔,例如:圖片、音樂、字型等。

4.res:可以存放專案中所有的資原始檔,例如:圖片(*.png*.jpg)、文字等。

5.res/drawable-hdpi:儲存高解析度圖片資源,可以使用Resources.getDrawable(id)可以獲得資源型別。

6.res/drawable-ldpi:儲存低分辯率圖片資源,可以使用Resources.getDrawable(id)可以獲得資源型別。

7.res/drawable-mdpi

:儲存中等解析度圖片資源,可以使用Resources.getDrawable(id)可以獲得資源型別。

8.res/layout:存放所有的佈局檔案,主要是用於排列不同的顯示元件,在Android程式中要讀取此配置。

9.res/values存放一些資原始檔的資訊,用於讀取文字資源,在本資料夾之中有一些約定的檔名稱:

定義如下:

<?xmlversion="1.0" encoding="utf-8" ?>

<resources>     <string-arrayname="MyArray">         

<itemname="1,35,3">

第一</item>          

<itemname="5,34,0">第二</item>          

<itemname="1,30,1">第三</item>           

<itemname="2,31,0">第四</item>    

</string-array></resources>

在程式碼中讀取:

Resources r =this.getResources();

String[] ary =r.getStringArray(R.array.MyArray);

   · colors.xml:定義表示顏色的資料;

   · dimens.xml:定義尺度,可以使用Resources.getDimension()獲得這些資源;

   · strings.xml:定義字串,可以使用Resources.getString()Resources.getText()方法獲得這些資源;

   · styles.xml:定義顯示的樣式檔案;

   <resourcesxmlns:android="http://schemas.android.com/apk/res/android">   

     <!--         Base application theme, dependent onAPI level. This theme is replaced       

                    by AppBaseTheme fromres/values-vXX/styles.xml on newer devices.    -->   

    <style name="AppBaseTheme"parent="android:Theme.Light">       

          <!--             Theme customizations available innewer API levels can go in           

                            res/values-vXX/styles.xml, while customizations related to           

                             backward-compatibilitycan go here.         -->     

    </style>   

<!--Application theme. -->   

     <style name="AppTheme"parent="AppBaseTheme">       

          <itemname="android:windowNoTitle">true</item>   

     </style>   

<!-- 全域性的Activity切換動畫 -->   

   <style name="MyTheme"parent="AppTheme">   

        <itemname="android:windowAnimationStyle">@style/ActivityAnimation</item>

   </style>   

   <stylename="ActivityAnimation">       

<!--  開啟activity,設定activity進入展示動畫 -->       

     <itemname="android:activityOpenEnterAnimation">@anim/pubblico_activity_anim_push_right_in</item>       

<!--  開啟activity,設定上個activity離開動畫 -->         

     <itemname="android:activityOpenExitAnimation">@anim/pubblico_activity_anim_push_left_out</item>       

<!--  結束activity,設定上個activity進入動畫 -->         

     <itemname="android:activityCloseEnterAnimation">@anim/pubblico_activity_anim_push_left_out</item>       

<!--  結束activity,設定當前activity離開動畫 -->         

     <item name="android:activityCloseExitAnimation">@anim/pubblico_activity_anim_push_right_out</item>     

  </style>

</resources>

10.res/raw:自定義的一些原生檔案所在目錄,像音樂、視訊等檔案格式。存放直接複製到裝置中的任意檔案。它們無需編譯,新增到你的應用程式編譯產生的壓縮檔案中。要使用這些資源,可以呼叫Resources.openRawResource(),引數是資源的ID也可以用,即R.raw.somefilenameResources.getRawResource()方法可以獲得這些資源。

11.res/xml:使用者自定義的XML檔案,所有的檔案在程式執行時編譯到應用程式之中,在程式執行時可以使用Resources.getXML()方法獲取。

12.res/anim:用於定義動畫物件。存放定義了補間動畫(tweenedanimation)或逐幀動畫(frameby frameanimation)的XML檔案。(該目錄下也可以存放定義property animationsXML檔案,但是最好還是分開存放)

13.res/animator:存放定義了propertyanimationsandroid 3.0新定義的動畫框架)的XML檔案

14.res/color/:存放定義了顏色狀態列表資源(ColorState List Resource)XML檔案

15.res/drawable/:存放定義了圖片狀態列表資源(ColorState List Resource)XML檔案

16.res/menu/:存放定義了應用程式選單資源的XML檔案。正確建立檔案的方法:new Folder,名字menunew Other——Android——XML,選擇menu型別,填寫名稱,確定即可。選單資原始檔必須放在res/menu目錄中。選單資原始檔必須使用<menu>標籤作為根節點。除了<menu>標籤外,還有另外兩個標籤用於設定選單項和分組,這兩個標籤是<item><group>

注意:

對於res/color和res/drawable這兩個檔案中的內容在這作一下詳解:

首先說一下color資料夾,在這個資料夾下放的是color_selector.xml等檔案,主要是用於背景色的selector,比如TextView中的textColor屬性,點選改變TextView中的字型顏色,在這個檔案中的檔案color_selector.xml中定義如下:

<?xmlversion="1.0" encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    <item

        android:state_window_focused="false" 

       android:color="@color/not_pressed" />

    <item

       android:state_selected="false"

        android:state_focused="false"

        android:state_pressed="false"

       android:color="@color/not_pressed"/>

    <item

        android:state_selected="true"

       android:color="@color/pressed" />

    <item

        android:state_focused="true"

       android:color="@color/pressed" />

    <item

        android:state_pressed="true"

        android:color="@color/pressed"/>

</selector>

主要的屬性是android:color引用不同的色值,而這些色值是在values/color.xml檔案中定義的,比如:

<?xmlversion="1.0" encoding="utf-8"?>

<resources>

   <color name="not_pressed">#000000</color>

   <color name="pressed">#ffffff</color>

</resources>

同時也可以發現,在color_selector.xml中沒有android:drawable屬性,我不知道Android中是根據什麼判斷什麼時候有android:drawable屬性,什麼時候有android:color屬性的

下面來看一下res/drawable資料夾中的檔案,這個資料夾中的檔案drawable_selector.xml,主要是用於背景圖的使用,Button中的android:background,點選改變背景,drawable_selecor.xml檔案的定義:

<?xmlversion="1.0" encoding="UTF-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    <item

       android:state_window_focused="false" 

       android:drawable="@drawable/info_collect_btn_normal_bg"/>

    <item

       android:state_selected="false"

        android:state_focused="false"

        android:state_pressed="false"

       android:drawable="@drawable/info_collect_btn_normal_bg" />

    <!-- Non focused states -->

    <item

        android:state_selected="true"

        android:drawable="@drawable/info_collect_btn_pressed_bg"/>

    <!-- Focused states -->

    <item

        android:state_focused="true"

       android:drawable="@drawable/info_collect_btn_pressed_bg" />

    <!-- Pressed -->

    <item

        android:state_pressed="true"

        android:drawable="@drawable/info_collect_btn_pressed_bg"/>

</selector>

這裡的info_collect_btn_normal_bg和info_collect_btn_pressed_bg是圖片資源,放在drawableXX資料夾下的。

同時也發現了在drawable_selector.xml中沒有android:color屬性,結合上面的color_selector.xml中沒有android:drawable屬性,知道了Android應該是根據資料夾來判斷的,在color資料夾下的檔案有android:color屬性,在drawable資料夾下的檔案有android:drawable屬性

同時還要注意的是:像textColor和background屬性引用到的檔案不能亂引用,比如textColor引用了drawable_selector.xml,background引用了color_selector.xml會報錯的。所以要注意使用。

但是現在有個問題:假設Button現在想點選變成綠色,不點選變成紅色,這時候怎麼辦?

首先肯定要用background的屬性-->那肯定就要用drawable_selector.xml檔案引用,所以在drawable_selector_.xml中將android:drawable="@drawable/info_collect_btn_normal_bg"改成android:drawable="@color/not_pressed"即可,這裡可能有個誤解就是android:drawable只能引用drawable資源,但是事實證明這個誤解是多餘的!