您的位置:首页 > 产品设计 > UI/UE

android常用的资源文件--values文件夹内文件

2016-10-24 11:20 525 查看
Android资源文件--values夹下文件及用法
以下文件皆在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放大像素一般用于设置文字的大小
长度转换:1 pt = 1/72 in

长度单位:

符号名称
pt
in英寸
mm毫米
cm厘米
V>bools.xml文件,定义一个布尔型的文件,使用方法同上。
<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 调用系统提供的暗灰色。这一获取方式仅仅获取系统提供的颜色,样式的获取也可以采用这种方法式,至于详细的讲解,以后再继续补充。希望对大家的学习和开发能有用,不足之处请大家不起赐教,谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息