android中xml文件的作用及解释

来源:互联网 发布:淘宝网开店 编辑:程序博客网 时间:2024/06/04 19:52

众所周知,XML是一种可扩展标记语言,它被用来传输和存储数据。在Android中也会随处可见XML文件,包括一个android项目不可缺少的AndroidManifest.xml清单文件,res资源文件目录下的anim/drawable/layout/menu/values中等,目录截图如下。其中清单文件中内容最多最复杂,完全可以在其他文章中再来讲解,所以本文主要讲解res目录下的XML的作用与内容。


一、anim目录

anim目录下的xml主要是用于android中的动画,包括Frame animation(逐帧动画)与Tween animation(补间动画 )。

1.逐帧动画

逐帧动画是一系列图片按照一定的顺序展示的过程,和放电影的机制很相似。可以理解成GIF,一帧一帧的显示图片。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:oneshot="false">  
  4.   <item android:drawable="@drawable/a_01" android:duration="50"/>  
  5.   <item android:drawable="@drawable/a_02" android:duration="50"/>  
  6.   <item android:drawable="@drawable/a_03" android:duration="50"/>  
  7.   <item android:drawable="@drawable/a_04" android:duration="50"/>  
  8.   <item android:drawable="@drawable/a_05" android:duration="50"/>  
  9.   <item android:drawable="@drawable/a_06" android:duration="50"/>  
  10. </animation-list>  
<animation-list>元素是必须的,并且必须要作为根元素,可以包含一或多个元素;android:onshot如果定义为true的话,此动画只会执行一次,如果为false则一直循环;元素代表一帧动画, android:drawable指定此帧动画所对应的图片资源;android:druation代表此帧持续的时间, 整数,单位为毫秒。

2. 补间动画

补间动画包括旋转、 平移、缩放和透明度等效果。
代码:
① 旋转
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/accelerate_interpolator">  
  4.   <!--fromDegrees:开始的角度  
  5.   toDegrees: 结束的角度, +表示是正的   
  6.   pivotX: 用于设置旋转时的x轴坐标 例 当值为"50",表示使用绝对位置定位 当值为"50%",表示使用相对于控件本身定位 当值为"50%p",表示使用相对于控件的父控件定位   
  7.   pivotY: 用于设置旋转时的y轴坐标 -->   
  8.   <rotate   
  9.     android:fromDegrees="0"   
  10.     android:toDegrees="+360"   
  11.     android:pivotX="50%"   
  12.     android:pivotY="50%"   
  13.     android:duration="1000"/>  
  14. </set>  


② 平移
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <!--  
  5.     始x轴坐标  
  6.     止x轴坐标  
  7.     始y轴坐标  
  8.     止y轴坐标缩放  
  9.     -->  
  10.     <translate  
  11.       android:fromXDelta="0%"  
  12.       android:toXDelta="100%"  
  13.       android:fromYDelta="0%"  
  14.       android:toYDelta="100%"  
  15.       android:duration="2000"/>  
  16. </set>  
③ 缩放
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <!--  
  5.     起始x轴坐标  
  6.     止x轴坐标  
  7.     始y轴坐标  
  8.     止y轴坐标  
  9.     x轴的坐标  
  10.     y轴的坐标  
  11.     -->  
  12.     <scale  
  13.       android:fromXScale="1.0"  
  14.       android:toXScale="0.0"  
  15.       android:fromYScale="1.0"  
  16.       android:toYScale="0.0"  
  17.       android:pivotX="50%"  
  18.       android:pivotY="50%"  
  19.       android:duration="1000"/>  
  20. </set>  
④ 透明度
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:interpolator="@android:anim/accelerate_interpolator">  
  4.     <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->  
  5.     <alpha  
  6.       android:fromAlpha="1.0"  
  7.       android:toAlpha="0.0"  
  8.       android:startOffset="500"  
  9.       android:duration="500"/>  
  10. </set>  

二、drawable目录

drawable目录主要是为了定义图片、按钮的背景及其点击状态。主要使用shape标签和selector标签。

1.shape标签

shape主要是定义一个形状,然后可以设置给某个按钮作为背景,最常用的就是圆角按钮。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"        
  3.       android:shape=["rectangle"|"oval"|"line"|"ring"] >   
  4.   
  5.      <!-- 圆角 -->       
  6.      <corners            
  7.             android:radius="integer"            
  8.             android:topLeftRadius="integer"            
  9.             android:topRightRadius="integer"            
  10.             android:bottomLeftRadius="integer"            
  11.             android:bottomRightRadius="integer" />    
  12.       <!-- 渐变 -->        
  13.       <gradient            
  14.             android:angle="integer"            
  15.             android:centerX="integer"            
  16.             android:centerY="integer"            
  17.             android:centerColor="integer"            
  18.             android:endColor="color"            
  19.             android:gradientRadius="integer"            
  20.             android:startColor="color"            
  21.             android:type=["linear"|"radial"|"sweep"]            
  22.             android:useLevel=["true"|"false"] />        
  23.       <padding            
  24.             android:left="integer"            
  25.             android:top="integer"            
  26.             android:right="integer"            
  27.             android:bottom="integer" />        
  28.       <size            
  29.             android:width="integer"            
  30.             android:height="integer" />        
  31.       <solid            
  32.             android:color="color" />    
  33.       <!-- 描边 -->     
  34.       <stroke            
  35.             android:width="integer"            
  36.             android:color="color"            
  37.             android:dashWidth="integer"            
  38.             android:dashGap="integer" />    
  39. </shape>  

2.selector标签

selector主要是定义不同状态按钮的背景等。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8" ?>     
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">   
  3.     <!-- 默认时的背景图片-->    
  4.     <item android:drawable="@drawable/a_01" />      
  5.     <!-- 没有焦点时的背景图片 -->    
  6.     <item android:state_window_focused="false"     
  7.           android:drawable="@drawable/a_01" />   
  8.     <!-- 非触摸模式下获得焦点并单击时的背景图片 -->    
  9.     <item android:state_focused="true"   
  10.           android:state_pressed="true"     
  11.           android:drawable="@drawable/a_02" />   
  12.     <!-- 触摸模式下单击时的背景图片-->    
  13.     <item android:state_focused="false"   
  14.           android:state_pressed="true"          
  15.           android:drawable="@drawable/a_03" />    
  16.     <!--选中时的图片背景-->    
  17.     <item android:state_selected="true"     
  18.           android:drawable="@drawable/a_04" />     
  19.     <!--获得焦点时的图片背景-->    
  20.     <item android:state_focused="true"    
  21.           android:drawable="@drawable/a_05" />    
  22. </selector>  

三、layout目录

layout目录主要存放android的布局文件,包括android中的五大布局:LinearLayout(线性布局)、FrameLayout(帧布局)、RelativeLayout(相对布局)、AbsoluteLayout(绝对布局)和TableLayout(表格布局)。这里就不在做详细讲解,相信大家在使用时也没有太大问题。

四、menu目录

menu目录主要用来存放菜单的样式,包括点击手机底部的菜单键和顶部actionbar中设置的菜单按钮时的弹出框的菜单项。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <menu xmlns:android="http://schemas.android.com/apk/res/android">    
  3.     <item android:id="@+id/connect"    
  4.           android:orderInCategory="100"    
  5.           android:showAsAction="never"    
  6.           android:icon="@android:drawable/ic_menu_send"    
  7.           android:title="连接" />    
  8.     <item android:id="@+id/disconnect"    
  9.           android:orderInCategory="100"    
  10.           android:showAsAction="never"    
  11.           android:icon="@android:drawable/ic_menu_close_clear_cancel"    
  12.           android:title="断开" />    
  13.     <item android:id="@+id/search"    
  14.           android:orderInCategory="100"    
  15.           android:showAsAction="never"    
  16.           android:icon="@android:drawable/ic_menu_search"    
  17.           android:title="发现" />    
  18.     <item android:id="@+id/view"    
  19.           android:orderInCategory="100"    
  20.           android:showAsAction="never"    
  21.           android:icon="@android:drawable/ic_menu_view"    
  22.           android:title="查看" />    
  23.     <item android:id="@+id/help"    
  24.           android:orderInCategory="100"    
  25.           android:showAsAction="never"    
  26.           android:icon="@android:drawable/ic_menu_help"    
  27.           android:title="帮助" />   
  28.     <item android:id="@+id/exit"    
  29.           android:orderInCategory="100"    
  30.           android:showAsAction="never"    
  31.           android:icon="@android:drawable/ic_menu_revert"    
  32.           android:title="退出" />    
  33. </menu>    
效果:

五、values目录

values目录下的东西比较多,包括arrays.xml/colors.xml/dimens.xml/ids.xml/strings.xml/styles.xml,如下图所示:

1.arrays.xml

arrays.xml文件中用于放各种数组数据,比如字符串数组、整型数组等,数组中的数据可能是具体的值,也有可能是对资源数据的引用。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string-array name="select_items">  
  4.         <item>one</item>  
  5.         <item>two</item>  
  6.         <item>three</item>  
  7.         <item>four</item>  
  8.     </string-array>  
  9. </resources>  
使用:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. String[] items = getResources().getStringArray(R.array.select_items);  
items数组中的数据就是arrays.xml文件中对应资源id R.array.selec_items中的数据。

2.colors.xml

colors.xml文件中主要用来说明需要的颜色值,也可以在res目录下另外新建一color文件夹用来存放这些xml文件
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.      <color name="red">#ff00000</color>    
  4.      <color name="black">#000000</color>  
  5.      <color name="white">#ffffff</color>    
  6. </resources>  
使用:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. btn.setBackgroundColor(getResources().getColor(R.color.red));  

3.dimens.xml

dimens.xml用来定义控件的尺寸和文字的大小,在其中定义是为了方便做屏幕适配。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resources>  
  2.    <!-- 控件的大小 -->  
  3.    <dimen name="title_width">200dp</dimen>  
  4.    <dimen name="title_height">50dp</dimen>  
  5.      
  6.    <!-- 字体的大小 -->  
  7.    <dimen name="info_size">20sp</dimen>  
  8.    <dimen name="tip_size">16sp</dimen>  
  9. lt;/resources>  
使用:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <TextView  
  2.         android:layout_width="@dimen/title_width"  
  3.         android:layout_height="@dimen/title_height"  
  4.         android:textSize="@dimen/info_size"/>  

4.ids.xml

ids.xml为应用的相关资源提供唯一的资源id。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <item name="send" type="id"/>  
  4.     <item name="public" type="id"/>  
  5. </resources>  
使用:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <TextView  
  2.         android:id="@id/send"  
  3.         android:layout_width="@dimen/title_width"  
  4.         android:layout_height="@dimen/title_height"  
  5.         android:textSize="@dimen/info_size"/>  

5.strings.xml

Android建议将在屏幕上显示的文字定义在strings.xml中,而且这样做也可以做到国际化。
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">TestDemo</string>  
  4.     <string name="action_add">添加</string>  
  5.     <string name="action_del">删除</string>  
  6.     <string name="action_settings">设置</string>  
  7.     <string name="action_about">关于</string>  
  8.     <string name="action_suggest">建议反馈</string>  
  9. </resources>  
使用:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <TextView  
  2.         android:id="@id/send"  
  3.         android:layout_width="@dimen/title_width"  
  4.         android:layout_height="@dimen/title_height"  
  5.         android:textSize="@dimen/info_size"  
  6.         android:text="@string/action_add"/>  

6.styles.xml

styles.xml主要用来存放android的主题与样式
代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resources>  
  2.     <style name="myDialog" parent="@android:style/Theme.Dialog">  
  3.         <item name="android:windowBackground">@color/transparent</item>  
  4.         <!-- 设置dialog背景 -->  
  5.         <item name="android:windowNoTitle">true</item>  
  6.         <!-- 无标题 -->  
  7.         <item name="android:windowIsFloating">true</item>  
  8.     </style>  
  9. </resources>  







0 0
原创粉丝点击