android常用的资源文件--values文件夹内文件
2016-10-24 11:20
525 查看
Android资源文件--values夹下文件及用法
以下文件皆在values文件夹下。例如:全路径:res/values/string.xml。
I>strings.xml文件,定义一些需要在开发中使用的字符串变量和数组,用来实现国际化,使用方法分别为:R.string.自己命名的名称、@string/自己命名的名称。
在xxx.java文件中使用方法:getResource().getString(R.string.app_name);
在xxx.xml文件中使用方法:android:text=“@string/app_name”
II>array.xml文件,定义存放一些数组的内容,使用方法同上。
在xxx.java文件中使用方法:getResource().getStringArray(R.arry.color);
在xxx.xml文件中使用方法:android:entries=“@array/color”(注:为spinner添加数组初始值)
III>colors.xml文件,主要存放一些自定义的颜色,使用方法同上。
在xxx.java文件中使用方法:getResource().getColor(R.color.RED);
在xxx.xml文件中使用方法:android:background=“@color/RED”
IV>dimens.xml文件,主要定义一些尺寸,使用方法同上。
使用举例:在xxx.xml文件中使用方法:android:background=“@dimens/horizontal_margin”
尺寸的单位:
长度转换:1 pt = 1/72 in
长度单位:
V>bools.xml文件,定义一个布尔型的文件,使用方法同上。
VI>styles.xml文件,放置样式的文件,可以使自己定义的样式,也可存放法系统的样式,样式可以应用用在窗口、控件、布局、主题设置中,但是必须与控件(View)的属性保持一致。定义样式分为两种:
格式一:<style name="定义当前的样式/主题的名称(主要用于引用)"> <item name="属性名称">属性值</item></style>
格式二:<style name="定义当前的样式/主题的名称(主要用于引用)" parent=“父样式名称”> <item name="属性名称">属性值</item></style>
总结:以上为values文件夹下常用的文件属性的设置,还用其他的属性,通常在属性赋值的时候直接进行赋值,降低消耗,提高效率。常用的属性通过调用名称赋值,方便代码的维护,简化代码,避免出现“重复造轮”的现象。以上都是自己命名并设置的属性,其实android系统本身也为开发者提供了一些属性,例如:@android:color/darker_gray 调用系统提供的暗灰色。这一获取方式仅仅获取系统提供的颜色,样式的获取也可以采用这种方法式,至于详细的讲解,以后再继续补充。希望对大家的学习和开发能有用,不足之处请大家不起赐教,谢谢。
以下文件皆在values文件夹下。例如:全路径:res/values/string.xml。
I>strings.xml文件,定义一些需要在开发中使用的字符串变量和数组,用来实现国际化,使用方法分别为:R.string.自己命名的名称、@string/自己命名的名称。
<resources> <!--属性name="自己命名的名称"--> <string name="app_name">Android4.0</string> <string_array name="ball"> <item name="basketball">篮球</item> <item name="soccer">足球</item> </resources>使用举例:
在xxx.java文件中使用方法:getResource().getString(R.string.app_name);
在xxx.xml文件中使用方法:android:text=“@string/app_name”
II>array.xml文件,定义存放一些数组的内容,使用方法同上。
<resources> <array name="color"> <item>#000</item> <item>#fff</item> </array> </resources>使用举例:
在xxx.java文件中使用方法:getResource().getStringArray(R.arry.color);
在xxx.xml文件中使用方法:android:entries=“@array/color”(注:为spinner添加数组初始值)
III>colors.xml文件,主要存放一些自定义的颜色,使用方法同上。
<resources> <color name="RED">#f00</color> </resources>使用举例:
在xxx.java文件中使用方法:getResource().getColor(R.color.RED);
在xxx.xml文件中使用方法:android:background=“@color/RED”
IV>dimens.xml文件,主要定义一些尺寸,使用方法同上。
<resources> <dimen name="horizontal_margin">15dp</dimen> </resources>
使用举例:在xxx.xml文件中使用方法:android:background=“@dimens/horizontal_margin”
尺寸的单位:
符号 | 名称 | 用法 |
dp | 独立像素 | 与设备大小无关 |
dx | 像素 | 与设备大小无关 |
sp | 放大像素 | 一般用于设置文字的大小 |
长度单位:
符号 | 名称 |
pt | 磅 |
in | 英寸 |
mm | 毫米 |
cm | 厘米 |
<resources> <bool name="flag_on">true</bool> </resources>
VI>styles.xml文件,放置样式的文件,可以使自己定义的样式,也可存放法系统的样式,样式可以应用用在窗口、控件、布局、主题设置中,但是必须与控件(View)的属性保持一致。定义样式分为两种:
格式一:<style name="定义当前的样式/主题的名称(主要用于引用)"> <item name="属性名称">属性值</item></style>
<style name="mystyle"> <item name="android:layout_width">match_parent</item> <itme name="android:textSize">30sp</item> </style>使用举例:在xxx.xml文件中使用方法:android:style=“@style/mystyle”在清单文件中使用:<application theme="@style/mystyle" ...> ...</application>...
格式二:<style name="定义当前的样式/主题的名称(主要用于引用)" parent=“父样式名称”> <item name="属性名称">属性值</item></style>
<style name="mystylew2" parent="mystyle"> <item name="android:textcolor">#0f0</item> </style> <style name="mystyle.mystyle2"> </item name="android:textColor">#0f0</item> </style>使用举例:在xxx.xml文件中使用方法:android:style=“@style/mystyle”在清单文件中使用:<application theme="@style/mystyle" ...> ...</application>...
总结:以上为values文件夹下常用的文件属性的设置,还用其他的属性,通常在属性赋值的时候直接进行赋值,降低消耗,提高效率。常用的属性通过调用名称赋值,方便代码的维护,简化代码,避免出现“重复造轮”的现象。以上都是自己命名并设置的属性,其实android系统本身也为开发者提供了一些属性,例如:@android:color/darker_gray 调用系统提供的暗灰色。这一获取方式仅仅获取系统提供的颜色,样式的获取也可以采用这种方法式,至于详细的讲解,以后再继续补充。希望对大家的学习和开发能有用,不足之处请大家不起赐教,谢谢。
相关文章推荐
- Android资源文件及文件夹介绍
- Android资源文件及文件夹介绍
- android中从values/strings资源文件中获取拼接字符串的方法
- Android资源文件夹下面values/style.xml、values-v19/style.xml、values-v21/style.xml主题调用规则
- 如何把使用到android res文件夹下面资源(R.xx.xx)的工程打包成jar文件,供其它项目使用
- Android资源文件及文件夹介绍
- Android资源文件及文件夹介绍
- Android读写assets目录下面的资源文件(文件夹)
- Eclipse添加Android项目工程后res文件夹下layout/values/drawable文件不可见
- android 读取assets文件夹下的文件资源
- android读写assets目录下面的资源文件(文件夹)
- Android中主要资源文件及文件夹介绍
- android读写assets目录下面的资源文件(文件夹)
- Android语言国际化values资源文件命名规则
- Android 资源文件夹下的文件不能有大写字符,会导致R.java无法生成
- Android 常用代码---资源文件
- android如何获取res文件夹下drawable文件夹下的图片资源文件转成一个BitMap对象
- 无法引用android资源文件夹中raw文件夹中的文件
- Android在资源文件values限定符
- Android 常用颜色资源文件color.xml