Android学习之常见的布局方式
来源:互联网 发布:双十一前淘宝生意差 编辑:程序博客网 时间:2024/06/06 09:17
第一种:线性布局
这种布局相对是比较简单的,要么竖向排列,要么横向排列
属性分别为: android:orientation= " horizontal " android:orientation= "vertical" 。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:padding="10px">
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Type:"/>
- <EditText
- android:id="@+id/et_entry"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#ffffff"/>
- <Button
- android:id="@+id/ok"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/et_entry"
- android:text="OK"/>
- <Button
- android:id="@+id/cancel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Cancel"/>
- </LinearLayout>
第二种:相对布局
相对布局:即相对于一个参照物的位置,那么必须先有参照物,才能确定接下来的控件的位置,例如先有A,然后B相对于A,在A的右边、下边或者什么位置。当然android中也可以相对于父窗体。
附加几类 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 离某元素上边缘的距离
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="100px"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/tv_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10px" <span style="font-family: Arial, Helvetica, sans-serif;">/*距离父窗体的左边10个像素*/</span>
- android:layout_marginTop="10px" <span style="font-family: Arial, Helvetica, sans-serif;">/*距离父窗体的顶部10个像素*/</span>
- android:textColor="#660000" /*控件上显示文本的颜色RGB*/
- android:textSize="20px" /*控件上显示文本的字体大小*/
- android:text="我是大的文本" />
- <TextView
- android:layout_below="@id/tv_title" /*当前控件位于tv_title这个控件的下方*/
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10px"
- android:layout_marginTop="10px"
- android:textColor="#660000"
- android:textSize="14px"
- android:text="我是小的文本" />
- <CheckBox
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true" /*当前控件与父窗体的右边对齐*/
- android:layout_centerVertical="true" /*当前控件在父窗体的竖直中心位置*/
- />
- </RelativeLayout>
这个布局如下图所示:
第三种:表格布局
表格布局:比如几行几列的格式,例如excel的样子
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="#000000"
- android:stretchColumns="1" > //它的意思就是自动拉伸第1列
- <TableRow >
- <TextView
- android:text="姓名"
- android:padding="10dip"
- android:textColor="#ffffff"/>
- <EditText
- android:layout_marginLeft="20dp"
- android:background="#ffffff"
- />
- </TableRow>
- <TableRow >
- <TextView
- android:text="密码"
- android:padding="10dip"
- android:textColor="#ffffff"/>
- <EditText
- android:layout_marginLeft="20dp"
- android:background="#ffffff"
- android:password="true"
- />
- </TableRow>
- </TableLayout>
第四种:帧布局
帧布局:其实比较简单的理解就是,一个图片叠加到一个图片的上面,就是图片的叠加
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="center"
- android:background="#ff0000" />
- <TextView
- android:layout_width="250dp"
- android:layout_height="390dp"
- android:layout_gravity="center"
- android:background="#dd0000" />
- <TextView
- android:layout_width="180dp"
- android:layout_height="300dp"
- android:layout_gravity="center"
- android:background="#bb0000" />
- <TextView
- android:layout_width="110dp"
- android:layout_height="210dp"
- android:layout_gravity="center"
- android:background="#990000" />
- <TextView
- android:layout_width="40dp"
- android:layout_height="120dp"
- android:layout_gravity="center"
- android:background="#770000" />
- </FrameLayout>
- Android学习之常见的布局方式
- Android学习之常见的布局方式
- Android学习之常见的布局方式--布局
- Android常见的布局方式
- android 常见布局方式
- Android学习 (六) 常见布局之线性布局
- ANDROID基础学习笔记_2_几种常见的布局方式
- Android学习系列(二)布局管理器之线性布局的3种实现方式
- Android横竖屏改变布局的常见两种方式
- 安卓开发常见布局方式学习
- Android学习--Android的常见控件与布局
- android学习之ListView布局的学习
- Android的布局方式
- Android--布局方式(LinearLayout)学习
- Android--布局方式(RelativeLayout)学习
- Android布局方式(FrameLayout)学习
- Android布局方式(AbsoluteLayout)学习
- Android布局方式(TableLayout)学习
- 在ios5下运行ios6项目时,报错
- 黑马程序员——Java概述
- 恢复window远程复制功能
- temp2
- byte[]转16进制bug总结
- Android学习之常见的布局方式
- iOS开发之键盘类型UIKeyboardType
- 一次奇怪的ORA-12519 TNS:no appropriate service handler found
- Gas Station
- NYOJ 1118【蓝翔校长的难题】
- CSS 给DIV添加四周边框阴影
- Memcached源码分析之内存管理
- iOS开发 - 手势识别(UIGestureRecognizer)
- /etc/rc.d/rc与/etc/rc.d/init.d的关系介绍