android内所使用的资源类型

最近没事逛android 官网,没想到变化不少。所以对android内所使用的一些资源样式作一些记录
Bool(布尔值)
    XML资源存储一个布尔值。
Color(颜色)
    XML资源存储颜色值(十六进制的颜色)。
Dimension(尺寸)
    XML资源进行一个维度值(度量单位)。
ID(android system 和app layout id标识)
    XML资源,提供应用程序资源和组件的唯一标识符。
Integer(整数)
    XML资源存储一个整数值。
Integer Array(整数数组)
    XML资源存储一个整数数组。
Typed Array(类型数组)
    XML的资源,提供了一个的TypedArray
下面对每个资料的类型解析
Bool 资料文件目录
res/values/bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <bool name="bool_name" >[true | false]</bool>
</resources>
代码引用和xml引及通过Resource对象获取
button.setClickable(R.bools.bool_name);//设置button是否点击
android:chickable="@bools/bool_name";//设置button是否点击
Resources res = new Resources();
boolean flag = res.getBoolean(R.bools.bool_name);
button.setClickable(flag);//设置button是否点击
Color 资料文件目录
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
        <color name="color_name" >RGB颜色</color>
</resources>
代码引用和xml引及通过Resource对象获取
button.setR.colors.color_name); //设置button是否点击
android:Background="@colors/color_name";//设置button是否点击
Resources res = new Resources();
int color = res.getColor(R.colors.color_name);
button.setBackground(flag);//设置button是否点击
Dimension 资料文件目录
尺寸大小有六种情况
dp---此单位相当160dip屏幕(英寸),1dp等于1px像素,使用dp可以很好的适配布局在不同密度的屏幕
sp---此单位适用对不同密度的屏幕大小的view字体大小的设置
pt---此单位points - 1/72英寸屏幕的物理尺寸的基础上(原始英寸屏幕像素点1/72)
px---此单位像素px - 对应屏幕上的实际像素,此单位不建议使用,用dp替代
mm---此单位毫米mm - 基于屏幕的物理尺寸
in---此单位英寸In - 基于屏幕的物理尺寸
res/values/dimen/dimens.cml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="dimension_name">尺寸大小</dimen>
</resources>
代码引用和xml引及通过Resource对象获取
button.setTextSize(R.dimens.dimension_name);
android:TextSize="@dimens/dimension_name";
Resources res = getResources();
float fontSize = res.getDimension(R.dimens.dimension_name);
button.setTextSize(fontSize);
ID 资料文件目录
res/values/ids.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="id_name" />
</resources>
代码和xml引用
button.setId(R.ids.id_name);
android:Id="@id/id_name"
Integer 资料文件目录
res/values/integers.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="max_speed">75</integer>
    <integer name="min_speed">5</integer>
</resources>
代码和xml引用
textview.setmaxLine(R.integers.max_speed);
android:textmaxLine="@integer/max_speed";
Resources res = getResources();
int maxSpeed = res.getInteger(R.integer.max_speed);
textview.setmaxLine(maxSpeed);
Integer Array 资料文件目录
res/values/array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="integer_array_name">
<item>integer值</item>
</integer-array>
</resources>
代码和xml引用
R.array.integer_array_name;
Resources res = getResources();
int[] bits = res.getIntArray(R.array.integer_array_name);
Type Array 资料文件目录
res/values/arrys.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="integer_array_name">
<item>resource</item>
</array>
</resources>
例如;
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>
代码引用
Resources res = getResources();
TypeArray icons = res.obtainTypeArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypeArray colors = res.getobtainTypeArray(R.array.colors);
int colors = colors.getColor(0,0);

你可能感兴趣的:(android,style,type,resource)