您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 其它相关文档 > 物联网系列专业课程:Android用户界面开发
Internet+EducationSolutionsAndroid用户界面开发中智讯(武汉)科技有限公司EducationSolutions目录ContentsInternet+用户界面基础用户界面布局用户界面控件05:18/3用户界面界面如同人的面孔,具有吸引用户的直接优势…05:18/4Android应用程序模型Android应用程序资源源代码Activity控件布局(Layout)图片(Drawable)值(Values)R.java05:18/5如何实现Android界面Android界面布局Layout定义了界面中所有的元素、结构和相互关系:使用XML文件描述界面布局在程序运行时动态添加或修改界面布局布局对象在程序中的调用:setContentView(…)从布局文件加载布局及控件findViewById(…)从资源中找到控件布局Activity加载setContentView使用findViewByID05:18/6AndroidXML布局文件使用XML文件声明界面布局的特点将程序的表现层和控制层分离在后期修改用户界面时,无需更改程序的源代码用户还能够通过可视化工具直接看到所设计的用户界面,有利于加快界面设计的过程,并且为界面设计与开发带来极大的便利性05:18/7简单的XML布局文件线性布局布局方向控件命名空间控件资源ID控件大小控件显示内容05:18/8界面对象的常规属性android:id属性:声明了控件对象的ID,这个ID主要用于在代码中引用这个控件对象“@+id/button1”表示所设置的ID值@表示后面的字符串是ID资源加号(+)表示需要建立新资源名称,并添加到R.java文件中斜杠后面的字符串(button1)表示新资源的名称如果资源不是新添加的,或属于Android框架的ID资源,则不需要使用加号(+),但必须添加Android包的命名空间,例如android:id=@android:id/emptyandroid:id=@+id/button105:18/9界面对象的常规属性android:layout_width属性:用来设置控件对象的宽度,fill_parent表示控件对象的宽度将等于父控件的宽度android:layout_height属性:用来设置控件对象的高度,wrap_content表示控件对象的宽度只要能够包含所显示的字符串即可android:layout_width=fill_parentandroid:layout_height=wrap_content05:18/10界面对象的常规属性Android中使用的单位px:表示屏幕实际的像素数。例如,320*480的屏幕在横向有320个象素,在纵向有480个象素。in:表示英寸,是屏幕的物理尺寸。每英寸等于2.54厘米,形容手机屏幕大小用的是屏幕的对角线长度。mm:表示毫米,是屏幕的物理尺寸。pt:表示一个点,是屏幕的物理尺寸,大小为1英寸的1/72。dp(密度独立像素):也作dip,是一种基于屏幕密度的抽象单位。基本密度是160dpi,如果屏幕密度提高,则dp对应的实际px数也会相应的提高。sp(scale独立像素):用于字体的一种基于屏幕密度的抽象单位。android:layout_width=“120dp05:18/11界面对象的常规属性android:text属性:用来设置控件对象上显示的文字内容android:text=Button05:18/12引入资源引入资源:将download.png文件拷贝到/res/drawable文件夹下在/res目录上选择Refresh新添加的文件将显示在/res/drawable文件夹下R.java文件内容也得到了更新否则提示无法找到资源的错误05:18/13程序加载XML资源通过调用方法:setContentView()可以实现对Layout布局资源的调用:publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);}05:18/14程序创建对象及引用通过调用方法:findViewById()可以实现对控件对象的引用:ButtonmyButton=(Button)findViewById(R.id.button1);myButton.setText(test);05:18/15可视化编辑器EducationSolutions目录ContentsInternet+用户界面基础用户界面布局用户界面控件05:18/17Android界面布局Android界面布局Layout定义了界面中所有的元素、结构和相互关系。05:18/18常用的布局对象Android操作系统提供下列5种窗体布局:线性布局(LinearLayout)框架布局(FrameLayout)表格布局(TableLayout)相对布局(RelativeLayout)绝对布局(AbsoluteLayout)05:18/19线性布局-LinearLayout线性布局LinearLayout在线性布局中,所有的子元素都按照垂直或水平的顺序在界面上排列如果垂直排列,则每行仅包含一个界面元素如果水平排列,则每列仅包含一个界面元素Android:orientation=“vertical”或Android:orientation=“horizontal”05:18/20线性布局实例LinearLayoutxmlns:android=:layout_width=fill_parentandroid:layout_height=fill_parentandroid:orientation=verticalTextViewandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=FirstName/EditTextandroid:layout_width=fill_parentandroid:layout_height=wrap_content/Buttonandroid:id=@+id/button1android:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:text=OK//LinearLayout05:18/21线性布局控件常用属性属性功能android:id为控件指定相应的IDandroid:text指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串android:gravity指定控件的基本位置,比如说居中,居右等位置android:textSize指定控件当中字体的大小android:background指定该控件所使用的背景色,RGB命名法android:width指定控件的宽度android:height指定控件的高度android:padding指定控件的内边距,也就是说控件当中的内容android:sigleLine如果设置为真的话,则将控件的内容在同一行当中进行显示,屏幕宽度不够时,将显示省略号…android:layout_weight控件的权重,以父控件为整体单位来计算05:18/22框架布局-FrameLayout框架布局FrameLayout框架布局(FrameLayout)是最简单的界面布局,是用来存放一个元素的空白空间,且子元素的位置是不能够指定的,只能够放置在空白空间的左上角如果有多个子元素,后放置的子元素将遮挡先放置的子元素使用AndroidSDK中提供的层级观察器(HierarchyViewer)进一步分析界面布局,能够对用户界面进行分析和调试,并以图形化的方式展示树形结构的界面布局05:18/23表格布局-TableLayout表格布局TableLayout表格布局(TableLayout)是一种常用界面布局,它将屏幕划分网格,通过指定行和列将界面元素添加到网格中网格的边界对用户是不可见的表格布局还支持嵌套,可以将另一个表格布局放置在前一个表格布局的网格中,也可以在表格布局中添加其他界面布局,例如线性布局、相对布局等TextViewEditTextRow1ButtonButtonRow2表格布局05:18/24相对布局-RelativeLayout相对布局RelativeLayout相对布局(RelativeLayout)是一种非常灵活的布局方式,能够通过指定界面元素与其他元素的相对位置关系,确定界面中所有元素的布局位置特点:能够最大程度保证在各种屏幕类型的手机上正确显示界面布局05:18/25绝对布局-AbsoluteLayout绝对布局AbsoluteLayout绝对布局(AbsoluteLayout)能通过指定界面元素的坐标位置,来确定用户界面的整体布局绝对布局是一种不推荐使用的界面布局,因为通过X轴和Y轴确定界面元素位置后,Android系统不能够根据不同屏幕对界面元素的位置进行调整,降低了界面布局对不同类型和尺寸屏幕的适应能力05:18/26布局的嵌套布局的嵌套是指相同或者不同类型的布局之间可以嵌套使用,其目的是为了利用不同布局的特性,方便构建我们想要得到的图案05:18/27布局嵌套实例?xmlversion=1.0encoding=utf-8?LinearLayoutxmlns:android=:layout_width=fill_parentandroid:layout_height=fill_parentandroid:orientation=verticalLinearLayoutandroid:layout_width=fill_parentandroid:layout_height=fill_parentandroid:layout_weight=1android:orientation=horizontalTextViewandroid:layout_width=wrap_contentandroid:layout_height=fill_parentandroid:layout_weight=1android:background=#aa0000android:gravity=center_horizontalandroid:text=red/TextViewandroid:layout_width=wrap_contentandroid:layout_height=fill_parentandroid:layout_weight=1android:background=#00aa00android:gravity=center_horizontalandroid:text=green/TextViewandroid:layout_width=wrap_contentandroid:layout_height=fill_parentandroid:layout_weight=1android:background=#0000aaandroid:gravity=center_horizontalandroid:text=blue//LinearLayoutLinearLayoutandroid:layout_width=fill_parentandroid:layout_height=fill_parentand
本文标题:物联网系列专业课程:Android用户界面开发
链接地址:https://www.777doc.com/doc-42699 .html