目录values下xml文件:TypeArray、attrs、styles、colors、strings、dimens

来源:互联网 发布:网络摄像头转模拟信号 编辑:程序博客网 时间:2024/06/14 17:32

1、attrs.xml:定义的是类的属性(声明自定义属性),这些属性会在类的构造函数中用到。这个还不太明白。贴个大神的链接。

以gallery为例吧,在attrs.xml中(注意此处必须是galleryItemBackground):

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <declare-styleable name="Gallery">  
  2.      <attr name="android:galleryItemBackground" />  
  3. </declare-styleable>  
在java文件构造函数中:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. TypedArray typeArray=obtainStyledAttributes(R.styleable.Gallery);  
  2. int backgroudId=typeArray.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);  

顺便说下TypeArray:是一组值的容器,用来存放通过obtainStyledAttributes(AttributeSet, int[], int, int) or obtainAttributes(AttributeSet, int[])得到的值。调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响。

2、styles.xml:定义各个控件的样式,样式由一个个属性组成。可在布局文件中引用。

styles.xml中定义:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <style name="ww">  
  2.     <item name="android:layout_width">wrap_content</item>  
  3.     <item name="android:layout_height">wrap_content</item>  
  4. </style>  
布局文件中引用:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. style="@style/ww"/>  

3、colors.xml:定义各种颜色值。

colors.xml中定义:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <color name="gray">#8E8D8E</color>  
布局文件中引用(所有用到颜色值的地方均可引用):
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. android:background="@color/gray"  

4、strings.xml:定义使用到的字符串变量。这个就不说了。

5、dimens.xml:定义尺寸。

dimens.xml中定义尺寸

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <dimen name="activity_horizontal_margin">16dp</dimen>  
布局文件中引用:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. android:paddingRight="@dimen/activity_horizontal_margin"  
       纵观以上,仅styles特殊。其余四个都是定义name,定义值;除styles对name有要求外(必须是属性名),其余三个均无要求。
0 0
原创粉丝点击