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:   存放一些資源文件的信息,用於讀取文本資源,在本文件夾之中有一些約定的文件名稱:

.attrs.xml:自定義屬性值的:具體可以參考 http://blog.csdn.net/jiangwei0910410003/article/details/17006087    · arrays.xml:定義數組數據;

定義如下:

<?xml version="1.0" encoding="utf-8" ?>

<resources>     <string-array name="MyArray">          

<item name="1,35,3">第一</item>           

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

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

<item name="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:定義顯示的樣式文件;

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

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

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

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

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

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

                             backward-compatibility can go here.         -->      

    </style>    

<!-- Application theme. -->    

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

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

     </style>    

<!-- 全局的Activity切換動畫 -->    

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

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

   </style>    

   <style name="ActivityAnimation">        

<!--  打開activity,設置activity進入展示動畫  -->        

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

<!--  打開activity,設置上個activity離開動畫  -->          

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

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

     <item name="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>
View Code

10.res/raw:自定義的一些原生文件所在目錄,像音樂、視頻等文件格式。存放直接復制到設備中的任意文件。它們無需編譯,添加到你的應用程序編譯產生的壓縮文件中。要使用這些資源,可以調用Resources.openRawResource(),參數是資源的ID也可以用,即R.raw.somefilename,Resources.getRawResource()方法可以獲得這些資源。

11.res/xml:用戶自定義的XML文件,所有的文件在程序運行時編譯到應用程序之中,在程序運行時可以使用Resources.getXML()方法獲取。

12.res/anim:用於定義動畫對象。存放定義了補間動畫(tweened animation)或逐幀動畫(frameby frame animation)的XML文件。(該目錄下也可以存放定義property animations的XML文件,但是最好還是分開存放)

13.res/animator:存放定義了propertyanimations(android 3.0新定義的動畫框架)的XML文件

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

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

16.res/menu/:存放定義了應用程序菜單資源的XML文件。正確創建文件的方法:new Folder,名字menu,new 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中定義如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns: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>
View Code
主要的屬性是android:color引用不同的色值,而這些色值是在values/color.xml文件中定義的,比如:
<?xml version="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文件的定義:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns: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>
View Code
這里的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資源,但是事實證明這個誤解是多余的!原理可以參考attr詳解中。

本文轉載自:http://blog.csdn.net/jiangwei0910410003/article/details/16985955


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM