Android资源访问.docx
- 文档编号:10312547
- 上传时间:2023-05-25
- 格式:DOCX
- 页数:16
- 大小:18.53KB
Android资源访问.docx
《Android资源访问.docx》由会员分享,可在线阅读,更多相关《Android资源访问.docx(16页珍藏版)》请在冰点文库上搜索。
Android资源访问
Android的资源访问
(基础版)
1、第一个android的应用程序创建目录结构
Src:
存放的是我们自己编写的java源代码
Gen:
R。
java自动生成,不需要我们编辑
Android2.3.3:
android本事的jar包
Assets:
存放文件例如:
视频、音频
Bin:
Res:
主要用来存放资源文件
Drawable:
图片资源
Layout:
布局资源
AndroidManifest。
Xml
二、主要内容
1、颜色资源
案例一:
颜色资源的使用
项目结构
工程名
Chapter03_resource_color
包名
Com.amaker.ch03.color
应用程序名称
TestColor
activity
TestColorActivity
项目代码:
第一步:
在res\values下新建一个color.xml
xmlversion="1.0"encoding="utf-8"?
>
第二步:
编辑res\layoutmain
xmlversion="1.0"encoding="utf-8"?
>
android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical"> android: text="测试颜色资源文件" android: textColor="@color/red_text"//引用颜色资源,在资源文件下引用的 android: layout_width="fill_parent" android: layout_height="wrap_content"/> 第三步: 改变程序的背景颜色 编辑TestcolorActivity.java packagecom.amaker.ch03.color; importandroid.app.Activity; importandroid.os.Bundle; publicclassTestcolorActivityextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ @Override //map键--值savedInstanceState: 保存状态 publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); getWindow().setBackgroundDrawableResource(R.color.blue_bag);//改变背景颜色 } } 2、字符串资源 案例二、字符串资源的使用 项目结构 工程名 Chapter03_resource_string 包名 Com.amaker.ch03.string 应用程序名称 Teststring activity TeststringActivity 项目代码 第一步: 编辑res\valuesstrings.xml xmlversion="1.0"encoding="utf-8"? > 第二步: 完成资源文件中引用字符串资源,编辑的main.xml xmlversion="1.0"encoding="utf-8"? > android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical"> android: id="@+id/MyTextView01" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="@string/test_str2"/>//资源中引用 android: id="@+id/MyTextView02" android: layout_width="fill_parent" android: layout_height="wrap_content" android: text=""/> 第三步: 在java代码中引用字符串资源,通过R.java作为中介 packagecom.amaker.ch03.string; importandroid.app.Activity; importandroid.os.Bundle; importandroid.widget.TextView; publicclassTeststringActivityextendsActivity{ privateTextViewmyTextView; /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); myTextView=(TextView)findViewById(R.id.MyTextView02); Stringstr=getString(R.string.test_str1).toString(); myTextView.setText(str); } } 3、尺寸资源 案例三、尺寸资源的使用 项目结构 工程名 Chapter03_Resource_dimens 包名 Com.amaker.ch03.dimens 应用程序名称 Testdimens activity TestdimensActivity 项目代码: 第一步: 定义尺寸资源文件res\values\dinem.xml xmlversion="1.0"encoding="utf-8"? > 第二步: 编辑main.xml,进过逐步调试,形成代码如下: xmlversion="1.0"encoding="utf-8"? > android=" android: layout_width="fill_parent" android: layout_height="fill_parent" android: orientation="vertical"> android: id="@+id/myDimenTextView01" android: layout_width="fill_parent" android: layout_height="wrap_content" android: width="@dimen/text_width" android: height="@dimen/text_heigh" android: text="@string/hello" android: background="#f00" />
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 资源 访问