笔记32--目录values下xml文件:TypeArray、attrs、styles、color
发布时间:2020-12-16 09:44:18 所属栏目:百科 来源:网络整理
导读:1、attrs.xml:定义的是类的属性(声明自定义属性),这些属性会在类的构造函数中用到。这个还不太明白。贴个大神的链接。 以gallery为例吧,在attrs.xml中(注意此处必须是galleryItemBackground): declare-styleable name="Gallery" attr name="android:gall
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有要求外(必须是属性名),其余三个均无要求。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |