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

本文详细介绍了Android项目中常用的XML资源文件,包括attrs.xml、styles.xml、colors.xml、strings.xml和dimens.xml的作用及使用方法。重点讲解了这些文件如何帮助开发者高效地管理和应用样式、颜色值、尺寸等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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有要求外(必须是属性名),其余三个均无要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值