【Android源码 栏目提醒】:网学会员Android源码 为您提供渴切-开源中文Android框架之Android控件及布局的使用大全 - 产品手册参考,解决您在渴切-开源中文Android框架之Android控件及布局的使用大全 - 产品手册学习中工作中的难题,参考学习。
Android 控件及布局的使用大全 目录第一章
Android 基础...................................................................................................................... 3 1.1
Android 开发环境搭建 .................................................................................................... 3 1.2
Android 项目的目录结构 ................................................................................................ 4 1.3 Hello World 程序 ............................................................................................................ 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.2 Service、Broadcast、BroadcastReceiver 的演示............................................................ 32 4.3
Android 控件 ..................................................................................................................... 37 4.3.1 TextView ................................................................................................................. 38 4.3.2 Button..................................................................................................................... 39 4.3.3 ImageButton........................................................................................................... 40 4.3.4 ImageView ............................................................................................................. 42 4.3.5 CheckBox ................................................................................................................ 43 4.3.6 RadioButton ........................................................................................................... 44 4.3.7 AnalogClock 钟表(带表盘) ............................................................................... 46 4.3.8 DigitalClock(电子表控件) ................................................................................. 47 4.3.9 DatePicker日期选择控件) ................................................................................. 48 4.3.10 TimePicker时间选择控件)............................................................................... 49 4.3.11 ToggleButton(双状态按钮控件) .................................................................... 50 4.3.12 EditText可编辑文本控件) ................................................................................ 51 4.3.13 ProgressBar进度条控件) ................................................................................. 52 4.3.14 SeekBar可拖动的进度条控件)........................................................................ 54 4.3.15 AutoCompleteTextView自动完成的可编辑文本控件) .................................. 56 4.3.16 MultiAutoCompleteTextView ............................................................................... 58 4.3.17 ZoomControls(放大/缩小按钮控件).............................................................. 60 4.3.18 Include(整合控件) .......................................................................................... 61 4.3.19 VideoView(视频播放控件) ............................................................................ 62 4.3.20 WebView(浏览器控件) .................................................................................. 64 4.3.21 RatingBar(评分控件) ...................................................................................... 65 4.3.22 Tab(选项卡控件) ............................................................................................ 66 4.3.23 Spinner(下拉框控件) ..................................................................................... 684.3.24 Chronometer(计时器控件) ............................................................................ 704.3.25 ScrollView(滚动条控件) ................................................................................. 724.3.26 TextSwitcher ......................................................................................................... 744.3.27 Gallery(缩略图浏览器控件) .......................................................................... 764.3.28 ImageSwitcher ...................................................................................................... 784.3.29 GridView(网格控件)....................................................................................... 814.3.30 ListView ................................................................................................................ 844.3.31 ExpandableList(支持展开/收缩功能的列表控件) ........................................ 88 第一章
Android 基础1.1
Android 开发环境搭建1、安装 JDK Java Development Kithttp://download.java.net/jdk6/2、安装
Android SDKhttp://developer.
android.com/sdk3、安装 Eclipsehttp://www.eclipse.org/4、打开 Eclipse ,并安装其
Android 插件(ADT)打开菜单 quotHelpquot -gt quotInstall New Softwarequot,在 quotAvailabe Softwarequot 中加入 地 址 http://dl-ssl.google.com/
android/eclipse/ , 然 后 安 装ADTAndroid Development Tools5、新建
Android 项目quotNewquot -gt
Android Project,Project Name - 项目名称;Build Target - 编译项目的 SDK 版本;Application name - 程序名称;Package name - 包名;MinSDK Version - 程序所支持的最低 SDK 版本代号(2 对应 1.1,3 对应 1.5,4对应 1.6)6、运行
Android 项目打开菜单 quotRunquot -gt quotRun Configurationsquot -gt New launch configuration,设置启动项目名称,在
Android 选项卡中选择启动项目,在 Target 选项卡中设置模拟器7、创建/使用模拟 SD 卡创 建 SD 卡 , 运 行 类 似 如 下 命 令 : mksdcard -l sdcard 512Md:androidsdcard.img模拟器中使用 SD 卡,在项目配置的 Target 选项卡的 quotAdditional EmulatorCommand Line Optionsquot 框 中 输 入 类 似 如 下 参 数 : -sdcardd:androidsdcard.img8、配置模拟器运行类似如下命令:
android create avd --name
android15 --target 2。
或者直接在菜单 quotWindowquot -gt quotAndroid AVD Managerquot 中配置模拟器9、浏览模拟 SD 卡中的内容调试程序,在 DDMS 中选择 quotFile Explorerquot ,在其中的 sdcard 目录下就是模拟 SD 卡中的内容10、查看日志 LogCatWindow -gt Show View -gt Other -gt
Android -gt LogCat11、在模拟器中安装/卸载 apk安装 apk 运行类似如下命令:adb install name.apk;卸载 apk 运行类似如下命令:adb uninstall packagename(注:这里的参数是需要卸载的包名)12、反编译
Android 程序解 压 apk 文 件 , 取 出 其 中 的 classes.dex 文 件 , 运 行 类 似 如 下 命 令 :dexdump.exe -d classes.dex gt dump.txt(其意思是将 classes.dex dump 出来,并将反编译后的代码保存到指定的文本文件中)13、人品不好是出现的某些错误的解决办法如果出现类似如下的错误等no classfiles specifiedConversion to Dalvik format failed with error 1解决办法:Project -gt Clean出现
Android SDK Content Loader 60 (一直卡在 60)解决办法:Project -gt 去掉 Build Automatically 前面的勾14、查看 SDK 源代码先 想 办 法 搞 到 源 代 码 , 如 这 个 地 址http://www.digginmobile.com/
android.asp ,然后将其解压到 SDK 根路径下的 sources 文件夹内即可1.2
Android 项目的目录结构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.3 Hello World 程序1、res/layout/main.xml 代码ltxml versionquot1.0quot encodingquotutf-8quotgtlt--设置 ID 的方式:ID 前加前缀,id/引用资源文件内字符串资源的方式:指定的资源名称前加前缀,string/--gtltLinearLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot
android:orientationquotverticalquot
android:layout_widthquotfill_parentquot
android:layout_heightquotfill_parentquot
android:idquotid/layoutquot gtltTextView
android:layout_widthquotfill_parentquot
android:layout_heightquotwrap_contentquot
android:textquotstring/helloquot /gtltTextView
android:layout_widthquotfill_parentquot
android:layout_heightquotwrap_contentquot
android:idquotid/txtquot /gtlt/LinearLayoutgt2、res/values/strings.xml 代码ltxml versionquot1.0quot encodingquotutf-8quotgtltresourcesgt ltstring namequothelloquotgtlayout 直接调用 values 中的字符串lt/stringgt ltstring namequothello2quotgt编程方式调用 values 中的字符串lt/stringgt ltstring namequotapp_namequotgtwebabcd_hellolt/stringgtlt/resourcesgt3、res/drawable 目录下放置一个名为 icon.png 的图片文件4、AndroidManifest.xml 代码ltxml versionquot1.0quot encodingquotutf-8quotgtltmanifest xmlns:androidquothttp://schemas.
android.com/apk/res/androidquot packagequotcom.webabcd.helloquot
android:versionCodequot1quot
android:versionNamequot1.0quotgt ltapplication
android:iconquotdrawable/iconquotandroid:labelquotstring/app_namequotgt ltactivity
android:namequot.Mainquot
android:labelquotstring/app_namequotgt ltintent-filtergt ltaction
android:namequotandroid.intent.action.MAINquot /gt ltcategoryandroid:namequotandroid.intent.category.LAUNCHERquot /gt lt/intent-filtergt lt/activitygt lt/applicationgt ltuses-sdk
android:minSdkVersionquot3quot /gtlt/manifestgt5、Main.java 代码package com.webabcd.helloimport
android.app.Activityimport
android.os.Bundleimport
android.widget.LinearLayoutimport
android.widget.TextViewpublic class Main extends Activity / Called when the activity is first created. / Override public void onCreateBundle savedInstanceState super.onCreatesavedInstanceState // 将指定的布局文件作为 Activity 所显示的内容 setContentViewR.layout.main // 动态地在指定的容器控件上添加新的控件 TextView txt new TextViewthis txt.setTextquot动态添加控件quot // setContentViewtxt LinearLayoutthis.findViewByIdR.id.layout.addViewtxt // 引用资源文件内的内容作为输出内容 TextView txt1 TextViewthis.findViewByIdR.id.txt txt1.setTextthis.getStringR.string.hello2 第二章 布局Layout和菜单Menu2.1 布局介绍在
Android 中各种布局的应用,以及菜单效果的实现各 种 布 局 方 式 的 应 用 , FrameLayout LinearLayout TableLayoutAbsoluteLayout RelativeLayout为指定元素配置上下文菜单,为应用程序配置选项菜单,以及多级菜单的实现1 、 各 种 布 局 方 式 的 演 示 ( FrameLayout LinearLayout TableLayoutAbsoluteLayout RelativeLayout)res/layout/main.xml 代码ltxml versionquot1.0quot encodingquotutf-8quotgtlt--layout_width - 宽fill_parent: 宽度跟着父元素走;wrap_content: 宽度跟着本身的内容走;直接指定一个 px 值来设置宽layout_height - 高;fill_parent: 高度跟着父元素走;wrap_content: 高度跟着本身的内容走;直接指定一个 px 值来设置高--gtlt--LinearLayout - 线形布局。
orientation - 容器内元素的排列方式; vertical: 子元素们垂直排列; horizontal: 子元素们水平排列 gravity - 内容的排列形式。
常用的有 top bottom left right center等,详见文档--gtltLinearLayoutxmlns:androidquothttp://schemas.
android.com/apk/res/androidquot
android:orientationquotverticalquot
android:gravityquotrightquot
android:layout_widthquotfill_parentquotandroid:layout_heightquotfill_parentquotgt lt-- FrameLayout - 层叠式布局。
以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示 --gt ltFrameLayout
android:layout_heightquotwrap_contentquot
android:layout_widthquotfill_parentquotgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquotandroid:textquotFrameLayoutquotgt lt/TextViewgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquot
android:textquotFrameLayoutquotgt lt/TextViewgt lt/FrameLayoutgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquotandroid:textquotstring/helloquot /gt lt-- TableLayout - 表格式布局。
TableRow - 表格内的行,行内每一个元素算作一列 collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“”隔开 stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多个用“”隔开 shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会自动收缩)的列的列索引,多个用“”隔开 --gt ltTableLayout
android:idquotid/TableLayout01quot
android:layout_widthquotfill_parentquotandroid:layout_heightquotwrap_contentquot
android:collapseColumnsquot1quotgt ltTableRow
android:idquotid/TableRow01quotandroid:layout_widthquotfill_parentquot
android:layout_heightquotwrap_contentquotgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_weightquot1quotandroid:layout_heightquotwrap_contentquot
android:textquot行 1 列 1quot /gt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_weightquot1quotandroid:layout_heightquotwrap_contentquot
android:textquot行 1 列 2quot /gt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_weightquot1quotandroid:layout_heightquotwrap_contentquot
android:textquot行 1 列 3quot /gt lt/TableRowgt ltTableRow
android:idquotid/TableRow01quotandroid:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquotgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquot
android:textquot行2 列 1quot /gt lt/TableRowgt lt/TableLayoutgt lt-- AbsoluteLayout - 绝对定位布局。
layout_x - x 坐标。
以左上角为顶点 layout_y - y 坐标。
以左上角为顶点 --gt ltAbsoluteLayout
android:layout_heightquotwrap_contentquot
android:layout_widthquotfill_parentquotgt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquotandroid:textquotAbsoluteLayoutquot
android:layout_xquot100pxquot
android:layout_yquot100pxquot /gt lt/AbsoluteLayoutgt lt-- RelativeLayout - 相对定位布局。
layout_centerInParent - 将当前元素放置到其容器内的水平方向和垂 直 方 向 的 中 央 位 置 ( 类 似 的 属 性 有 : layout_centerHorizontallayout_alignParentLeft 等) layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离 layout_below - 放置当前元素到指定的元素的下面 layout_alignRight - 当前元素与指定的元素右对齐 --gt ltRelativeLayout
android:idquotid/RelativeLayout01quot
android:layout_widthquotfill_parentquotandroid:layout_heightquotfill_parentquotgt ltTextView
android:layout_widthquotwrap_contentquotandroid:idquotid/abcquot
android:layout_heightquotwrap_contentquotandroid:textquotcenterInParenttruequot
android:layout_centerInParentquottruequot /gt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquotandroid:textquotmarginLeft20pxquot
android:layout_marginLeftquot20pxquot /gt ltTextView
android:layout_widthquotwrap_contentquot
android:layout_heightquotwrap_contentquot
android:textquotxxxquot
android:layout_belowquotid/abcquotandroid:layout_alignRightquotid/abcquot /gt lt/RelativeLayoutgtlt/LinearLayoutgtres/values/strings.xmlltxml versionquot1.0quot encodingquotutf-8quotgtltresourcesgt ltstring namequothelloquotgtHello L.
上一篇:
毕业设计论文-基于Android平台的商城(含代码)
下一篇:
7*30m公路预应力混凝土连续梁桥上部结构设计