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

来源:互联网 发布:笔记本防蓝光软件 编辑:程序博客网 时间:2024/05/20 15:36
<p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">1.src:存放所有的*.java源程序。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">2.gen:为ADT插件自动生成的代码文件保存路径,里面的R.java将保存所有的资源ID。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">3.assets:可以存放项目一些较大的资源文件,例如:图片、音乐、字体等。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">4.res:可以存放项目中所有的资源文件,例如:图片(*.png、*.jpg)、文本等。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">5.res/drawable-hdpi:保存高分辨率图片资源,可以使用Resources.getDrawable(id)可以获得资源类型。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">6.res/drawable-ldpi:保存低分辩率图片资源,可以使用Resources.getDrawable(id)可以获得资源类型。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">7.res/drawable-mdpi:保存中等分辨率图片资源,可以使用Resources.getDrawable(id)可以获得资源类型。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">8.res/layout:存放所有的布局文件,主要是用于排列不同的显示组件,在Android程序中要读取此配置。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">9.res/values:   存放一些资源文件的信息,用于读取文本资源,在本文件夹之中有一些约定的文件名称:</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">   .attrs.xml:自定义属性值的:具体可以参考 <a target=_blank target="_blank" href="http://write.blog.csdn.net/postedit/17006087" style="color: rgb(255, 153, 0); text-decoration: none; "></a><a target=_blank target="_blank" href="http://blog.csdn.net/jiangwei0910410003/article/details/17006087" style="color: rgb(255, 153, 0); text-decoration: none; ">http://blog.csdn.net/jiangwei0910410003/article/details/17006087</a>   · arrays.xml:定义数组数据;</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">     定义如下:</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; "><span style="color: rgb(255, 0, 0); ">     <?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></span></p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; "><span style="color: rgb(255, 0, 0); "> 在代码中读取:</span></p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; "><span style="color: rgb(255, 0, 0); ">Resources r = this.getResources();String[] ary = r.getStringArray(R.array.MyArray);</span></p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">   · colors.xml:定义表示颜色的数据;   · dimens.xml:定义尺度,可以使用Resources.getDimension()获得这些资源;   · strings.xml:定义字符串,可以使用Resources.getString()或Resources.getText()方法获得这些资源;   · styles.xml:定义显示的样式文件;</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; "><span style="color: rgb(255, 0, 0); "><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">    <span style="white-space: pre; "></span><item name="android:windowAnimationStyle">@style/ActivityAnimation</item><span style="white-space: pre; "></span></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></span><span style="color: rgb(51, 51, 51); ">10.res/raw</span><span style="color: rgb(51, 51, 51); ">:自定义的一些原生文件所在目录,像音乐、视频等文件格式。存放直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用</span><span style="color: rgb(51, 51, 51); ">Resources.openRawResource()</span><span style="color: rgb(51, 51, 51); ">,参数是资源的</span><span style="color: rgb(51, 51, 51); ">ID</span><span style="color: rgb(51, 51, 51); ">也可以用,即</span><span style="color: rgb(51, 51, 51); ">R.raw.<em>somefilename</em></span><span style="color: rgb(51, 51, 51); ">,</span><span style="color: rgb(51, 51, 51); ">Resources.getRawResource()</span><span style="color: rgb(51, 51, 51); ">方法可以获得这些资源。</span></p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">11.res/xml:用户自定义的XML文件,所有的文件在程序运行时编译到应用程序之中,在程序运行时可以使用Resources.getXML()方法获取。</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">12.res/anim:用于定义动画对象。存放定义了补间动画(tweened animation)或逐帧动画(frameby frame animation)的XML文件。(该目录下也可以存放定义property animations的XML文件,但是最好还是分开存放)</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">13.res/animator:存放定义了propertyanimations(android 3.0新定义的动画框架)的XML文件</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">14.res/color/:存放定义了颜色状态列表资源(ColorState List Resource)的XML文件</p><p align="left" style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(51, 51, 51); ">15.res/drawable/:存放定义了图片状态列表资源(ColorState List Resource)的XML文件</p><span style="color: rgb(51, 51, 51); ">16.res/menu/</span><span style="color: rgb(51, 51, 51); ">:存放定义了应用程序菜单资源的</span><span style="color: rgb(51, 51, 51); ">XML</span><span style="color: rgb(51, 51, 51); ">文件。正确创建文件的方法:</span><span style="color: rgb(51, 51, 51); ">new Folder</span><span style="color: rgb(51, 51, 51); ">,名字</span><span style="color: rgb(51, 51, 51); ">menu</span><span style="color: rgb(51, 51, 51); ">,</span><span style="color: rgb(51, 51, 51); ">new Other——Android——XML</span><span style="color: rgb(51, 51, 51); ">,选择</span><span style="color: rgb(51, 51, 51); ">menu</span><span style="color: rgb(51, 51, 51); ">类型,填写名称,确定即可。菜单资源文件必须放在</span><span style="color: rgb(51, 51, 51); ">res/menu</span><span style="color: rgb(51, 51, 51); ">目录中。菜单资源文件必须使用</span><span style="color: rgb(51, 51, 51); "><menu></span><span style="color: rgb(51, 51, 51); ">标签作为根节点。除了</span><span style="color: rgb(51, 51, 51); "><menu></span><span style="color: rgb(51, 51, 51); ">标签外,还有另外两个标签用于设置菜单项和分组,这两个标签是</span><span style="color: rgb(51, 51, 51); "><item></span><span style="color: rgb(51, 51, 51); ">和</span><span style="color: rgb(51, 51, 51); "><group></span><span style="color: rgb(51, 51, 51); ">。</span>
注意:
对于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"><span style="white-space: pre; "></span><item <span style="white-space: pre; "></span>    android:state_window_focused="false"  <span style="white-space: pre; "></span>    android:color="@color/not_pressed" /><span style="white-space: pre; "></span><item <span style="white-space: pre; "></span>    android:state_selected="false" <span style="white-space: pre; "></span>    android:state_focused="false" <span style="white-space: pre; "></span>    android:state_pressed="false" <span style="white-space: pre; "></span>    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文件中定义的,比如:
<?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"><span style="white-space: pre; "></span><item <span style="white-space: pre; "></span>    android:state_window_focused="false"  <span style="white-space: pre; "></span>    android:drawable="@drawable/info_collect_btn_normal_bg"/><span style="white-space: pre; "></span><item <span style="white-space: pre; "></span>    android:state_selected="false" <span style="white-space: pre; "></span>    android:state_focused="false" <span style="white-space: pre; "></span>    android:state_pressed="false" <span style="white-space: pre; "></span>    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资源,但是事实证明这个误解是多余的!原理可以参考attr详解中。
0 0
原创粉丝点击