Android控件及布局的使用大全.docx
- 文档编号:7423422
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:94
- 大小:36.11KB
Android控件及布局的使用大全.docx
《Android控件及布局的使用大全.docx》由会员分享,可在线阅读,更多相关《Android控件及布局的使用大全.docx(94页珍藏版)》请在冰豆网上搜索。
Android控件及布局的使用大全
Android控件及布局的使用大全
第一章Android基础......................................................................................................................3
1.1Android开发环境搭建....................................................................................................3
1.2Android项目的目录结构................................................................................................4
1.3HelloWorld程序............................................................................................................5
第二章布局(Layout)和菜单(Menu).............................................................................................8
2.1布局....................................................................................................................................8
2.2上下文菜单选项菜单子菜单...........................................................................................11
第三章组件...................................................................................................................................16
3.1对话框(Dialog)和通知(Notification).................................................................................16
3.1.1常用对话框的演示................................................................................................16
3.1.2各种提示效果的演示............................................................................................22
第四章活动、服务、广播、广播接收器...................................................................................27
4.1演示Activity的基本用法...............................................................................................27
4.2Service、Broadcast、BroadcastReceiver的演示............................................................32
4.3Android控件.....................................................................................................................37
4.3.1TextView.................................................................................................................38
4.3.2Button.....................................................................................................................39
4.3.3ImageButton...........................................................................................................40
4.3.4ImageView.............................................................................................................42
4.3.5CheckBox................................................................................................................43
4.3.6RadioButton...........................................................................................................44
4.3.7AnalogClock钟表(带表盘)...............................................................................46
4.3.8DigitalClock(电子表控件).................................................................................47
4.3.9DatePicker(日期选择控件).................................................................................48
第一章Android基础
1.1Android开发环境搭建
1、安装JDK(JavaDevelopmentKit)
2、安装AndroidSDK
3、安装Eclipse
4、打开Eclipse,并安装其Android插件(ADT)
打开菜单"Help"->"InstallNewSoftware",在"AvailabeSoftware"中加
ADT(AndroidDevelopmentTools)
5、新建Android项目
"New"->AndroidProject,ProjectName-项目名称;BuildTarget-编译
项目的SDK版本;Applicationname-程序名称;Packagename-包名;Min
SDKVersion-程序所支持的最低SDK版本代号(2对应1.1,3对应1.5,4
对应1.6)
6、运行Android项目
打开菜单"Run"->"RunConfigurations"->Newlaunchconfiguration,设
置启动项目名称,在Android选项卡中选择启动项目,在Target选项卡中设
置模拟器
7、创建/使用模拟SD卡
创建SD卡,运行类似如下命令:
mksdcard-lsdcard512M
d:
\android\sdcard.img
模拟器中使用SD卡,在项目配置的Target选项卡的"AdditionalEmulator
CommandLineOptions"框中输入类似如下参数:
-sdcard
d:
\android\sdcard.img
8、配置模拟器
运行类似如下命令:
androidcreateavd--nameandroid15--target2。
或者
直接在菜单"Window"->"AndroidAVDManager"中配置模拟器
9、浏览模拟SD卡中的内容
调试程序,在DDMS中选择"FileExplorer",在其中的sdcard目录下就是
模拟SD卡中的内容
10、查看日志LogCat
Window->ShowView->Other->Android->LogCat
11、在模拟器中安装/卸载apk
安装apk运行类似如下命令:
adbinstallname.apk;卸载apk运行类似如下
命令:
adbuninstallpackagename(注:
这里的参数是需要卸载的包名)
12、反编译Android程序
解压apk文件,取出其中的classes.dex文件,运行类似如下命令:
dexdump.exe-dclasses.dex>dump.txt(其意思是将classes.dexdump出
来,并将反编译后的代码保存到指定的文本文件中)
13、人品不好是出现的某些错误的解决办法
如果出现类似如下的错误等
noclassfilesspecified
ConversiontoDalvikformatfailedwitherror1
解决办法:
Project->Clean
出现AndroidSDKContentLoader60%(一直卡在60%)
解决办法:
Project->去掉BuildAutomatically前面的勾
14、查看SDK源代码
先想办法搞到源代码,如这个地址
的sources文件夹内即可
1.2Android项目的目录结构
1、src-用于放置源程序
2、gen-自动生成R.java文件,用于引用资源文件(即res目录下的数据)
3、assets-用于放置原始文件,Android不会对此目录下的文件做任何处理,
这是其与res目录不同的地方
4、res/drawable-用于放置图片之类的资源;res/layout-用于放置布局用
的xml文件;res/values-用于放置一些常量数据
5、AndroidManifest.xml-Android程序的清单文件,相当于配置文件,配置
应用程序名称、图标、Activity、Service、Receiver等
1.3HelloWorld程序
1、res/layout/main.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
--
设置ID的方式:
ID前加前缀,@+id/
引用资源文件内字符串资源的方式:
指定的资源名称前加前缀,@string/
-->
android: orientation="vertical" android: layout_width="fill_parent" android: layout_height="fill_parent" android: id="@+id/layout" > android: layout_width="fill_parent" android: layout_height="wrap_content" android: text="@string/hello" /> android: layout_width="fill_parent" android: layout_height="wrap_content" android: id="@+id/txt" />
2、res/values/strings.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
3、res/drawable目录下放置一个名为icon.png的图片文件
4、AndroidManifest.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
android:
versionCode="1"
android:
versionName="1.0">
icon="@drawable/icon" android: label="@string/app_name"> name=".Main" android: label="@string/app_name"> minSdkVersion="3"/> 5、Main.java 代码 import import import import publicclassMainextendsActivity{ /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); //将指定的布局文件作为Activity所显示的内容 //动态地在指定的容器控件上添加新的控件 TextViewtxt=newTextView(this); txt.setText("动态添加控件"); //setContentView(txt); //引用资源文件内的内容作为输出内容 } } 第二章布局(Layout)和菜单(Menu) 2.1布局 介绍在Android中各种布局的应用,以及菜单效果的实现 各种布局方式的应用,FrameLayout,LinearLayout,TableLayout, AbsoluteLayout,RelativeLayout 为指定元素配置上下文菜单,为应用程序配置选项菜单,以及多级菜单的实现 1、各种布局方式的演示(FrameLayout,LinearLayout,TableLayout, AbsoluteLayout,RelativeLayout) res/layout/main.xml 代码 xmlversion="1.0"encoding="utf-8"? > -- layout_width-宽; fill_parent: 宽度跟着父元素走; wrap_content: 宽度跟着本身的内容走;直接指定一个px值来设置宽 layout_height-高; fill_parent: 高度跟着父元素走; wrap_content: 高度跟着本身的内容走;直接指定一个px值来设置高 --> -- LinearLayout-线形布局。 orientation-容器内元素的排列方式; vertical: 子元素们垂直排列; horizontal: 子元素们水平排列 gravity-内容的排列形式。 常用的有top,bottom,left,right,center 等,详见文档 --> android: orientation="vertical"android: gravity="right" android: layout_width="fill_parent" android: layout_height="fill_parent"> -- FrameLayout-层叠式布局。 以左上角为起点,将 FrameLayout内的元素 一层覆盖一层地显示 --> layout_height="wrap_content" android: layout_width="fill_parent"> layout_width="wrap_content" android: layout_height="wrap_content" android: text="FrameLayout"> layout_width="wrap_content" android: layout_height="wrap_content"android: text="Frame Layout"> layout_width="wrap_content" android: layout_height="wrap_content" android: text="@string/hello"/> -- TableLayout-表格式布局。 TableRow-表格内的行,行内每一个元素算作一列 collapseColumns-设置TableLayout内的TableRow中需要隐藏的 列的列索引,多个用“,”隔开 stretchColumns-设置TableLayout内的TableRow中需要拉伸(该 列会拉伸到所有可用空间)的列的列索引,多个用“,”隔开 shrinkColumns-设置TableLayout内的TableRow中需要收缩(为 了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“,” 隔开 --> id="@+id/TableLayout01" android: layout_width="fill_parent" android: layout_height="wrap_content" android: collapseColumns="1"> id="@+id/TableRow01" android: layout_width="fill_parent" android: layout_height="wrap_content"> layout_width="wrap_content" android: layout_weight="1" android: layout_height="wrap_content" android: text="行1列1"/> layout_width="wrap_content" android: layout_weight="1" android: layout_height="wrap_content" android: text="行1列2"/> layout_width="wrap_content" android: layout_weight="1" android: layout_height="wrap_content" android: text="行1列3"/> id="@+id/TableRow01" android: layout_width="wrap_content" android: layout_height="wrap_content"> layout_width="wrap_content" android: layout_height="wrap_content"android: text="行 2列1"/> -- AbsoluteLayout-绝对定位布局。 layout_x-x坐标。 以左上角为顶点 layout_y-y坐标。 以左上角为顶点 --> layout_height="wrap_content" android: layout_width="fill_parent"> layout_width="wrap_content" android: layout_height="wrap_content" android: text="AbsoluteLayout" android: layout_x="100px" android: layout_y="100px"/> -- RelativeLayout-相对定位布局。 layout_centerInParent-将当前元素放置到其容器内的水平方向和 垂直方向的中央位置(类似的属性有: layout_centerHorizontal, layout_alignParentLeft等) layout_marginLeft-设置当前元素相对于其容器的左侧边缘的距离 layout_below-放置当前元素到指定的元素的下面 layout_alignRight-当前元素与指定的元素右对齐 --> id="@+id/RelativeLayout01" android: layout_width="fill_parent" android: layout_height="fill_parent"> layout_width="wrap_content" android: id="@+id/abc" android: layout_height="wrap_content" android: text="centerInParent=true" android: layout_centerInParent="true"/> layout_width="wrap_content" android: layout_height="wrap_content" android: text="marginLeft=20px" android: layout_marginLeft="20px"/> layout_width="wrap_content" android: layout_height="wrap_content"android: text="xxx" android: layout_below="@id/abc" android: layout_align
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 控件 布局 使用 大全
![提示](https://static.bdocx.com/images/bang_tan.gif)