Android项目中各个文件夹的含义和用途详解

来源:互联网 发布:什么软件检查电脑硬件 编辑:程序博客网 时间:2024/05/20 11:48


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:定义数组数据;

定义如下:

<?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资源,但是事实证明这个误解是多余的!

1 0