Android控件大全以及各布局空间的使用方式.docx
《Android控件大全以及各布局空间的使用方式.docx》由会员分享,可在线阅读,更多相关《Android控件大全以及各布局空间的使用方式.docx(103页珍藏版)》请在冰豆网上搜索。
Android控件大全以及各布局空间的使用方式
控件讲解
?
在Windows下搭建Android开发环境
?
Android项目的目录结构说明
?
写一个简单的HelloWorld程序
一、在Windows下搭建Android开发环境
1、安装JDK(JavaDevelopmentKit)
2、安装AndroidSDK
3、安装Eclipse
http:
//www.eclipse.org/
4、打开Eclipse,并安装其Android插件(ADT)
打开菜单"Help"->"InstallNewSoftware",在"AvailabeSoftware"中加入地址http:
//dl-,然后安装ADT(AndroidDevelopmentTools)
5、新建Android项目
"New"->AndroidProject,ProjectName-项目名称;BuildTarget-编译项目的SDK版本;Applicationname-程序名称;Packagename-包名;MinSDKVersion-程序所支持的最低SDK版本代号(2对应1.1,3对应1.5,4对应1.6)
6、运行Android项目
打开菜单"Run"->"RunConfigurations"->Newlaunchconfiguration,设置启动项目名称,在Android选项卡中选择启动项目,在Target选项卡中设置模拟器
7、创建/使用模拟SD卡
创建SD卡,运行类似如下命令:
mksdcard-lsdcard512Md:
\android\sdcard.img
模拟器中使用SD卡,在项目配置的Target选项卡的"AdditionalEmulatorCommandLineOptions"框中输入类似如下参数:
-sdcardd:
\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源代码
先想办法搞到源代码,如这个地址http:
//www.digginm
,然后将其解压到SDK根路径下的sources文件夹内即可
二、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等
三、HelloWorld程序
1、res/layout/main.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
--
设置ID的方式:
ID前加前缀,@+id/
引用资源文件内字符串资源的方式:
指定的资源名称前加前缀,@string/
-->
android="
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"?
>
layout直接调用values中的字符串
编程方式调用values中的字符串
webabcd_hello
3、res/drawable目录下放置一个名为icon.png的图片文件
4、AndroidManifest.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
android="
package="com.webabcd.hello"
android:
versionCode="1"
android:
versionName="1.0">
icon="@drawable/icon"android:
label="@string/app_name">
name=".Main"
android:
label="@string/app_name">
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
minSdkVersion="3"/>
5、Main.java
代码
packagecom.webabcd.hello;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.LinearLayout;
importandroid.widget.TextView;
publicclassMainextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//将指定的布局文件作为Activity所显示的内容
setContentView(R.layout.main);
//动态地在指定的容器控件上添加新的控件
TextViewtxt=newTextView(this);
txt.setText("动态添加控件");
//setContentView(txt);
((LinearLayout)this.findViewById(R.id.layout)).addView(txt);
//引用资源文件内的内容作为输出内容
TextViewtxt1=(TextView)this.findViewById(R.id.txt);
txt1.setText(this.getString(R.string.hello2));
}
}
四、系出名门Android
(2)-布局(Layout)和菜单(Menu)
介绍
在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="
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="FrameLayout">
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_alignRight="@id/abc"/>
res/values/strings.xml
xmlversion="1.0"encoding="utf-8"?
>
HelloLayout
webabcd_layout
Main.java
代码
packagecom.webabcd.layout;
importandroid.app.Activity;
importandroid.os.Bundle;
publicclassMainextendsActivity{
/**Calledwhentheactivityisfirstcreated.*/
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
2、上下文菜单,选项菜单,子菜单
res/layout/main.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
orientation="vertical"android:
layout_width="fill_parent"
android:
layout_height="fill_parent">
id="@+id/txt1"android:
layout_width="fill_parent"
android:
layout_height="wrap_content"android:
text="@string/hello_contextMenu"/>
id="@+id/txt2"android:
layout_width="fill_parent"
android:
layout_height="wrap_content"android:
text="@string/hello_subMenu"/>
res/values/strings.xml
代码
xmlversion="1.0"encoding="utf-8"?
>
HelloContextMenu
HelloContextSubMenu
webabcd_menu
Main.java
代码
packagecom.webabcd.menu;
importandroid.app.Activity;
importandroid.os.B