笔记32--目录values下xml文件:TypeArray、attrs、styles、colors、strings、dimens

来源:互联网 发布:照片叠加软件 编辑:程序博客网 时间:2024/06/05 09:31

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

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

<declare-styleable name="Gallery">     <attr name="android:galleryItemBackground" /></declare-styleable>
在java文件构造函数中:
TypedArray typeArray=obtainStyledAttributes(R.styleable.Gallery);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中定义:

<style name="ww">    <item name="android:layout_width">wrap_content</item>    <item name="android:layout_height">wrap_content</item></style>
布局文件中引用:
 style="@style/ww"/>

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

colors.xml中定义:

<color name="gray">#8E8D8E</color>
布局文件中引用(所有用到颜色值的地方均可引用):
android:background="@color/gray"

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

5、dimens.xml:定义尺寸。

dimens.xml中定义尺寸

<dimen name="activity_horizontal_margin">16dp</dimen>
布局文件中引用:
android:paddingRight="@dimen/activity_horizontal_margin"
       纵观以上,仅styles特殊。其余四个都是定义name,定义值;除styles对name有要求外(必须是属性名),其余三个均无要求。

0 0