您的位置:首页 > 移动开发 > Android开发

跟我学Android之七 资源文件

2016-07-03 22:37 381 查看
本章内容


第1节 字符串、颜色和尺寸资源

第2节 数组资源

第3节 Drawable资源

第4节 布局资源

第5节 样式和主题资源


本章目标


熟练掌握字符串、颜色和尺寸资源的使用。

熟练掌握数组资源的用法。

掌握各种Drawable资源的用法。

掌握布局资源的用法

掌握样式和主题资源的使用。


字符串常量资源


使用字符串常量资源,当有字符串常量需要在程序中使用时应该定义字符串常量资源,在res/values/string.xml中定义。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name=“okLabel”>确定</string>
</resources>
在XML布局中使用





<Button … android:text=“@string/okLabel” />
在java代码中使用

Button okBtn = (Button)findViewById(R.id.okBtn);
okBtn.setText(getString(R.string.okLabel));
颜色值通过RGB(红、绿、蓝)三原色和一个透明度(Alpha)值表示。

它必须以“#”开头,后面接Alpha-Red-Green-Blue形式的内容。其中,Alpha值可以省略,如果省略,表示颜色默认是完全不透明的

#RGB:使用红、绿、蓝三原色的值来表示颜色,其中,红、绿和蓝采用0~f来表示。例如,要表示红色,可以使用#f00。

#ARGB:使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、绿和蓝均采用0~f来表示。例如,要表示半透明的红色,可以使用#6f00。

#RRGGBB:使用红、绿、蓝三原色的值来表示颜色,与#RGB不同的是,这里的红、绿和蓝使用00—ff来表示。例如,要表示蓝色,可以使用#0000ff。

#AARRGGBB:使用透明度以及红、绿、蓝三原色来表示颜色,其中,透明度、红、绿均采用00-ff来表示。例如,要表示半透明的绿色,可以使用#6600ff00。


颜色常量资源的使用


颜色的定义是通过RGB三色和一个alpha值来定义的,#RGB、#ARGB、#RRGGBB、#AARRGGBB,在资源文件中定义颜色,一般在res/values下建议colors.xml文件,定义颜色如下:

<resources>
<color name=“red”>#ff0000</color>
</resources>


在代码中使用颜色

int color = Resources.getSystem().getColor(R.color.red);
btn.setBackgroundColor(color);


在布局文件中使用颜色





android:background="@color/red"



度量单位


属性中的度量单位

px(像素)不同设备的显示效果相同

in(英寸)长度单位

mm(毫米)长度单位

pt(磅)1/72英寸

dp(与密度无关的像素)

一种基于屏幕密度的抽象单位

在每英寸160个点的显示器上,1dp=1px

dip(与dp相同)

sp(与刻度无关的像素)

与dp类似,但是可以根据用户字体大小缩放

建议用sp用作字体大小的单位

使用尺寸常量资源,在尺寸常量资源中定义一些固定不变的尺寸信息,如:间隔,在res/values/dimens.xml中定义。





<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="horizontal_margin">16dp</dimen>
<dimen name="vertical_margin">16dp</dimen>
</resources>
在XML布局中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="@dimen/vertical_margin"
android:paddingLeft="@dimen/horizontal_margin“ …>
在代码中使用

int margin = getResources().getDimension(R.dimen. horizontal_margin);


数组资源



文件位于res\values目录下,根元素是<resources></resources>标记,在该元素中,包括以下3个子元素

<array>子元素:用于定义普通类型的数组。

<integer-array>子元素:用于定义整数数组。

<string-array>子元素:用于定义字符串数组

使用数组常量资源,当有固定内容的数组需要在程序中使用时可以定义数组常量资源,在res/values/arrays.xml中定义。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="booktype">
<item>语言类</item>
<item>工具类</item>
</string-array>
</resources>


在代码中使用,可以使用ID的地方直接使用ID就可以,在不方便使用ID的地方可以通过方法调用使用。

String[] array = getResources().getStringArray(R.array.booktype);


.9.png图片在android 环境下具有自适应调节大小的能力
shape可以用于定义各种UI的元素属性,通常在res/drawable下建立相应的XML文件用于定义shape,solid用于定义实心填充,比如:

<solid android:color="#ff9d77"/>

gradient用于定义渐变色,比如:

<gradient
android:startColor="#eb7720"
android:endColor="#ddbda5"
android:angle="270” />


stroke用于定义描边

<stroke
android:width="2dp"
android:color="#dcdcdc" />


布局资源文件


布局资源文件放置在res\layout目录下,布局资源文件的根元素通常是各种布管理器,在该布局管理器中,通常是各种View组件或是嵌套的其他布局管理器。

加载布局

setContentView(R.layout.main);


引入布局





<include layout=“@layout/image”/>


样式与主题概述

Android中的视图可以通过样式和主题进行外观的定制,通常在res/values下建立styles.xml来定义样式和主题,主题和样式都在使用style标签进行定义,通过name属性确定一个唯一的名字,通过parent属性指定用于继承的父类样式,通过子标签item定义各种样式的内容,示例如下:

使用样式来改变单个视图外观,比如:TextView

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mystyle">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
<item name="android:background">@drawable/shap_background</item>
</style>
</resources>


首先在res/values下建立styles.xml来定义样式
<resources>
<style name="textViewStyle” parent="textViewStyle">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">20sp</item>
</style>
<style name="testTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">?android:windowNoTitle</item>
</style>
</resources><strong>
</strong>


在TextView中使用样式

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world”  style="@style/mystyle” />


使用主题来改变整个应用的外观,当把样式用来改变应用或者Activity的时候就叫做主题了,首先在res/values下建立styles.xml来定义样式

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="mytheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">?android:windowNoTitle</item>
</style>
</resources>


在Application中使用主题

<application
android:allowBackup="true“ android:icon="@drawable/ic_launcher"
android:label="@string/app_name”  android:theme="@style/mytheme" >


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: