Android界面布局

来源:互联网 发布:关于时间的模型算法 编辑:程序博客网 时间:2024/06/07 02:33
     安卓软件开发的coding第一步就是设计界面了。
     界面设计包括布局和组件,组件按布局要求排列形成界面,而安卓的布局有以下五大布局:
【1】FrameLayout 框架布局,是布局文件中默认的最简单的布局。
   所有添加到这个布局中的视图都以层叠的方式显示,且组件均显示在屏幕的左上角。第一个添加的控件被放在最底层,最后一个添加到框架布局中的视图显示在最顶层,上一层的控件会覆盖下一层的控件。在非常简单的界面中常用。

【2】LinearLayout 线性布局
   在一个方向上(垂直或水平)对齐所有子元素,一个垂直列表每行将只有一个子元素(无论它们有多宽),一个水平列表只是一列的高度(最高子元素的高度来填充)。是常用的布局。
垂直线性布局:android:orientation="vertical"
水平线性布局:android:orientation="horizontal"
两者可嵌套使用。
例如:一个登录界面

<!--总体布局设计为垂直线性布局--><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><!--嵌套水平布局,放置文本框和数字输入框--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/text_username"/><EditText android:id="@+id/editUserName"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="number" /></LinearLayout><!--嵌套水平布局,放置文本框和密码输入框--><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal" ><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/text_password" /><EditText android:id="@+id/editPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="textPassword"/></LinearLayout><!--登录按钮--><Button android:id="@+id/btnLogin"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/btn_login" /></LinearLayout>


【3】RelativeLayout 相对布局
相对布局的子控件会根据它们所设置的参照控件和参数进行相对布局。参照控件可以是父控件,也可以是其它子控件,但是被参照的控件必须要在参照它的控件之前定义。个人觉得这种布局和线性布局比较好用,可常用到。
例如:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.ttkupao.MainActivity"    tools:ignore="MergeRootFrame" ><ImageButton    android:id="@+id/jump"    android:layout_width="80dp"    android:layout_height="80dp"    android:layout_alignParentBottom="true"    android:layout_alignParentRight="true"    android:background="@drawable/touch2"     android:onClick="click"/><ImageButton    android:id="@+id/down"    android:layout_width="80dp"    android:layout_height="80dp"    android:layout_alignParentBottom="true"    android:layout_alignParentLeft="true"    android:background="@drawable/touch"     android:onClick="click"/></RelativeLayout>

这是两个按钮放在屏幕的左下角和右下角位置,均是以父控件为参考的,这时没有必要将子控件之间再增加相对关系。
下面是相对布局的一些重要属性:
第一类:属性值为true或false
android:layout_centerHrizontal                       水平居中
android:layout_centerVertical                        垂直居中
android:layout_centerInparent                      相对于父元素完全居中
android:layout_alignParentBottom                     贴紧父元素的下边缘
android:layout_alignParentLeft                       贴紧父元素的左边缘
android:layout_alignParentRight                      贴紧父元素的右边缘
android:layout_alignParentTop                        贴紧父元素的上边缘
android:layout_alignWithParentIfMissing             如果对应的兄弟元素找不到的话就以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below                          在某元素的下方
android:layout_above                          在某元素的的上方
android:layout_toLeftOf                       在某元素的左边
android:layout_toRightOf                     在某元素的右边
android:layout_alignTop               本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft              本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom            本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight             本元素的右边缘和某元素的的右边缘对齐

第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom              离某元素底边缘的距离
android:layout_marginLeft                   离某元素左边缘的距离
android:layout_marginRight                 离某元素右边缘的距离
android:layout_marginTop                   离某元素上边缘的距离

【4】TableLayout(表格布局)
表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列,子组件全部按照水平线性排列,并且宽(match parent)高(wrap parent)一致,这样所有组件就像是排列在一个表格中,单元格可以为空,但是不可以跨列

【5】AbsoluteLayout(绝对布局)
android:layout_x="Xdp", android:layout_x="Xdp"指定组件的位置,即子控件需要指定相对于此坐标布局的横纵坐标值。由于手机应用需要适应不同的屏幕大小,而这种布局模型不能自适应屏幕尺寸大小,所以应用的相对较少。

具体的布局设计需要根据具体情况来设计,线性布局和相对布局是最常用到的。另外绘图的组件View或surfaceView若需要显示在其他组件之下,可在其他组件之前添加,否则可能被其他组件覆盖。
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 蛐蛐罐底翻砂了怎么办 剑网3中被盗号后怎么办 本人想离婚对方躲避怎么办 微信不能说语音怎么办 微信语音发不了怎么办 吃鸡语音用不了怎么办 要杀我的人见面怎么办 转晕了想吐怎么办 原地转圈头晕恶心想吐怎么办 孩子吃凉的呕吐头还晕怎么办 孩子转晕了想吐怎么办 转圈晕了想吐怎么办 我爸总是骂我妈怎么办 转圈转的想吐怎么办 大便干燥拉不出来怎么办 吹完头发很干燥怎么办 腿摔伤了结痂疼怎么办 蹭wifi被禁止后怎么办 电脑wifi给拉黑怎么办 电脑想用无线网怎么办 中路被对方打崩了怎么办 英雄联盟队友太坑怎么办 匹配被王者虐了怎么办 lol碰到嘴臭的怎么办 小婴儿脾气大怎么办呢? 玩游戏输入法会跳出出来怎么办 逆水寒fps太低怎么办 我dcj没地速怎么办 电焊看久眼睛疼怎么办 装修忘了窗帘盒怎么办? 纹眉导致眼肿了怎么办 哭泣引起的眼肿怎么办 在酒店忘记拉窗帘了怎么办 湿气重喉咙有痰怎么办 眼睛上火了肿了怎么办 陌陌直播没人看怎么办 陌陌直播没人气怎么办 我真的爱上你了怎么办 弯腰时间久了腰疼怎么办 斗鱼pk输的怎么办 领导当着人骂我怎么办