Android中的资源介绍(分为res和assets):
(本次讲解res中的资源---->)
概括地讲,Android的资源是指非代码部分。比如图片,mp3,字符串,xml文件等。
在一个Android工程中,和src源文件夹并列的有两个文件夹,分别叫做res和assets。这两个文件夹是用来保存资源文件的。
不同点:
1、res中的资源乐意通过R资源类之间访问。这种方式在Android中比较常用。
res中包含各种子文件夹,对资源进行分类;
anim(xml动画文件),drawable(图片),layout(布局文件),
menu(菜单),raw(二进制文件),values(常量值),
xml(xml文件)
2、assets中保存的一般是原始的文件,例如,mp3文件,android程序不能通过R类直接访问。必须通过AssertManager类以二进制的形式来读取。
学习res中的资源:
一般使用资源分为两种方式:
1、在代码中使用context的getResources()方法得到Resources对象,该对象提供了获得各种资源类型的方法;
2、在其他资源中引用资源的一般格式是这样的:
@[包名称:]资源类型/资源名称
‚R.资源类型.资源名称
1、颜色资源的使用:
颜色值的定义是通过(RGB三原色来定义)
颜色资源xml的定义:
代码:
colors.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="text_color" >#ff0000</color> <color name="bg_color1" >#00ff00</color> <color name="bg_color2">#ff0ff0</color> </resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textColor="@color/text_color"
android:background="@color/bg_color1"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="test"/>
</LinearLayout>
R.java:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package test.clolors;
public final class R {
public static final class attr {
}
public static final class color {
public static final int bg_color1=0x7f040001;
public static final int bg_color2=0x7f040002;
public static final int text_color=0x7f040000;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int button1=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello=0x7f050001;
}
}
Test_colorsActivity.java:
package test.clolors;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class Test_colorsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void test(View view){
int color=this.getResources().getColor(R.color.bg_color2);
Toast.makeText(this, ""+color, 1).show();
this.getWindow().setBackgroundDrawableResource(R.color.bg_color2);
}
}
运行结果:
2、字符串资源的使用:
在一个Android工程中,我们可能会使用到大量的字符串作为提示信息,这些字符串都可以作为字符串资源声明在配置文件中,从而实现程序的可配置性。
在代码中,我们使用Context.getString()方法,通过传递资源ID参数来得到改字符串,也可以在其资源文件中引用字符串资源,引用格式为“@string/字符串资源名称”。
两种引用方式,一种获取方式;
代码:
strings.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Test_string</string> <string name="hello">Hello World!</string> <string name="welcome">欢迎您,小熊熊!</string> </resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/welcome" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="test"/>
</LinearLayout>
Test_stringActivity.java:
package test.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Test_stringActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void test(View view){
String str = this.getString(R.string.welcome);
Toast.makeText(this, str, 1).show();
Button button =(Button) findViewById(R.id.button1);
button.setText(R.string.welcome);
}
}
运行结果:
3、尺寸资源的使用:
我们可以使用一些常用的尺寸单位来定义一些文字尺寸,试图组件的宽和高。尺寸资源是一个数字类型的数据,被定义在res\values\dimens.xml文件中;
代码:
dimens.xml:
<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="text_width">150px</dimen> <dimen name="text_height">100px</dimen> <dimen name="button_width">30mm</dimen> <dimen name="button_height">10mm</dimen> </resources>
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:width="@dimen/text_width"
android:height="@dimen/text_height"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:width="@dimen/button_width"
android:height="@dimen/button_height"
android:onClick="test"/>
</LinearLayout>
Test_dimenActivity.java:
package test.dimen;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Test_dimenActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void test(View view){
Button button = (Button)findViewById(R.id.button1);
float width = this.getResources().getDimension(R.dimen.text_width);
float height = this.getResources().getDimension(R.dimen.text_height);
button.setWidth((int)width);
button.setHeight((int)height);
}
}
运行结果: