常用五种布局方式,分别是:FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。
一、FrameLayout:所有东西依次都放在左上角,会重叠,这个布局比较简单,也只能放一点比较简单的东西。二、LinearLayout:线性布局,每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。三、AbsoluteLayout:绝对布局用X,Y坐标来指定元素的位置,这种布局方式也比较简单,但是在屏幕旋转时,往往会出问题,而且多个元素的时候,计算比较麻烦。四、RelativeLayout:相对布局可以理解为某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below、 android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh;五、TableLayout:表格布局,每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素。每一个布局都有自己适合的方式,这五个布局元素可以相互嵌套应用,做出美观的界面。
5大布局方式有以下几种:
- 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。
- 帧布局(FrameLayout):组件从屏幕左上方布局组件。
- 表格布局(TableLayout):按照行列方式布局组件。
- 相对布局(RelativeLayout):相对其它组件的布局方式。
- 绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。
线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。
常用的属性:
android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小
第一个实例
①效果图:
②核心代码如下:
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:gravity="right"
- >
- <!-- android:gravity="right"表示Button组件向右对齐 -->
- <Button
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="确定"
- />
- <Button
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:text="取消"
- />
- </LinearLayout>
- </LinearLayout>
第二个实例
①效果图:
②核心代码:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="red"
- android:gravity="center_horizontal"
- android:background="#aa0000"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <!--android:gravity="center_horizontal"水平居中 -->
- <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。
- 线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。
- 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,
- 那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;
- 对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,
- 再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度-->
- <TextView
- android:text="Teal"
- android:gravity="center_horizontal"
- android:background="#008080"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="blue"
- android:gravity="center_horizontal"
- android:background="#0000aa"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- <TextView
- android:text="orange"
- android:gravity="center_horizontal"
- android:background="#FFA500"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"
- />
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="row one"
- android:textSize="15pt"
- android:background="#aa0000"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <!-- -->
- <TextView
- android:text="row two"
- android:textSize="15pt"
- android:background="#DDA0DD"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <TextView
- android:text="row three"
- android:textSize="15pt"
- android:background="#008080"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- <TextView
- android:text="row four"
- android:textSize="15pt"
- android:background="#FFA500"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- />
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="300dp"
- android:layout_height="300dp"
- android:background="#00BFFF"
- />
- <TextView
- android:layout_width="260dp"
- android:layout_height="260dp"
- android:background="#FFC0CB"
- />
- <TextView
- android:layout_width="220dp"
- android:layout_height="220dp"
- android:background="#0000FF"
- />
- </FrameLayout>
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分
android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TableRow>
- <Button
- android:text="Button1"
- />
- <Button
- android:text="Button2"
- />
- <Button
- android:text="Button3"
- />
- </TableRow>
- <TableRow>
- <Button
- android:text="Button4"
- />
- <Button
- android:layout_span="2"
- android:text="Button5"
- />
- </TableRow>
- </TableLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:padding="10px"
- >
- <TextView
- android:id="@+id/tev1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="30dp"
- android:text="Please Type Here:"
- />
- <EditText
- android:id="@+id/tx1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_below="@id/tev1"
- />
- <Button
- android:id="@+id/btn1"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_below="@id/tx1"
- android:layout_alignParentRight="true"
- android:text="确定"
- />
- <Button
- android:id="@+id/btn2"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:layout_below="@id/tx1"
- android:layout_toLeftOf="@id/btn1"
- android:layout_marginRight="30dp"
- android:text="取消"
- />
- </RelativeLayout>
ViewGroup | Width/Height , Padding/margin |
FrameLayout | gravity |
LinearLayout |
Orientation/horizontal/vertical
weight
|
RelativeLayout |
alignBaseline/(Left/Top/Right/Bottom) Start/End
alignParent
center Horizontal/Vertical/InParent
below/above/toLeftOf/toRightOf/toStartOf/toEndOf
|
linearLayout.setLayoutParams(lp);
state_focused
state_selected
android:singleLine="true"
android:textColor=""
android:textSize=""
一、RelativeLayout布局属性 与 include、merge简单总结
// 当前视图顶部,底部,左侧,右侧与其他视图间填充区域
- android:layout_marginTop
- android:layout_marginBottom
- android:layout_marginLeft
- android:layout_marginRight
java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- // 单位是px
- layoutParams.topMargin = 66;
- layoutParams.bottomMargin = 66;
- layoutParams.leftMargin = 66;
- layoutParams.rightMargin = 66;
// 当前视图内顶部,底部,左侧,右侧填充区域 (非RelativeLayout属性)
- android:paddingTop
- android:paddingBottom
- android:paddingLeft
- android:paddingRight
// 在指定ID视图上面,下面,左侧,右侧
- android:layout_above
- android:layout_below
- android:layout_toLeftOf
- android:layout_toRightOf
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ABOVE, R.id.viewId);
- layoutParams.addRule(RelativeLayout.BELOW, R.id.viewId);
- layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.viewId);
- layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.viewId);
// 与制定ID视图baseLine,顶部,底部,左侧,右侧边缘对齐
- android:layout_alignBaseline
- android:layout_alignTop
- android:layout_alignBottom
- android:layout_alignLeft
- android:layout_alignRight
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.viewId);
// 与父视图顶部,底部,左侧,右侧边缘对齐
- android:layout_alignParentTop
- android:layout_alignParentBottom
- android:layout_alignParentLeft
- android:layout_alignParentRight
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// 横向居中,纵向居中,针对与父视图剧中
- android:layout_centerHorizontal
- android:layout_centerVertical
- android:layout_centerInParent
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
- layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
- layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
** Include 与 mege标签
参考资料:
Re-using Layouts with <include/>
Romain Guy Android Layout Tricks #3: Optimize, Part 1
** ViewGroup相关配置
1. clipChildren
android:clipChildren setClipChildren(boolean)Defines whether a child is limited to draw inside of its bounds or not.
定义是否限制子视图在它的范围内进行绘制。默认是true
2. clipToPadding
android:clipToPadding setClipToPadding(boolean)Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area.
定义ViewGroup是否将剪切它的绘制界面并排除padding区域。默认是true
参考资料:
android:layout_gravity
android:layout_gravity="right"
lp.gravity = Gravity.RIGHT;
button.setGravity(Gravity.CENTER);
xml配置:
设置EditText光标位置
http://www.cnblogs.com/nanhuchongzi/archive/2012/04/17/2444451.html
** margin与padding的区别?
view.getLayoutParams().width = widthValue;
http://blog.csdn.net/tu_bingbing/article/details/8654990
**TextView 显示两行,其余用....替换
android:maxLines="2"
android:ellipsize="end"
values/ids.xml
<resources>
<item type="id" name="idname" />
</resources>
view.setId(R.id. idname);
***
布局XML Schema
有哪些属性,是在XML Schema定义,可以在http://www.w3school.com.cn/schema/schema_intro.asp了解Schema更多知识。
Android 布局XML Schema在什么地方? 查询补漏
布局在Java中的对应关系 TypeArray
原文地址:http://blog.csdn.net/love_world_/article/details/8426935
2013-12-29 添加gravity、 layout_gravity、duplicateParentState
2014-04-10 添加布局XML Schema
ViewGroup | Width/Height , Padding/margin |
FrameLayout | gravity |
LinearLayout |
Orientation/horizontal/vertical
weight
|
RelativeLayout |
alignBaseline/(Left/Top/Right/Bottom) Start/End
alignParent
center Horizontal/Vertical/InParent
below/above/toLeftOf/toRightOf/toStartOf/toEndOf
|
linearLayout.setLayoutParams(lp);
state_focused
state_selected
android:singleLine="true"
android:textColor=""
android:textSize=""
一、RelativeLayout布局属性 与 include、merge简单总结
// 当前视图顶部,底部,左侧,右侧与其他视图间填充区域
- android:layout_marginTop
- android:layout_marginBottom
- android:layout_marginLeft
- android:layout_marginRight
java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- // 单位是px
- layoutParams.topMargin = 66;
- layoutParams.bottomMargin = 66;
- layoutParams.leftMargin = 66;
- layoutParams.rightMargin = 66;
// 当前视图内顶部,底部,左侧,右侧填充区域 (非RelativeLayout属性)
- android:paddingTop
- android:paddingBottom
- android:paddingLeft
- android:paddingRight
// 在指定ID视图上面,下面,左侧,右侧
- android:layout_above
- android:layout_below
- android:layout_toLeftOf
- android:layout_toRightOf
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ABOVE, R.id.viewId);
- layoutParams.addRule(RelativeLayout.BELOW, R.id.viewId);
- layoutParams.addRule(RelativeLayout.LEFT_OF, R.id.viewId);
- layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.viewId);
// 与制定ID视图baseLine,顶部,底部,左侧,右侧边缘对齐
- android:layout_alignBaseline
- android:layout_alignTop
- android:layout_alignBottom
- android:layout_alignLeft
- android:layout_alignRight
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ALIGN_BASELINE, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.viewId);
- layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.viewId);
// 与父视图顶部,底部,左侧,右侧边缘对齐
- android:layout_alignParentTop
- android:layout_alignParentBottom
- android:layout_alignParentLeft
- android:layout_alignParentRight
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
- layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// 横向居中,纵向居中,针对与父视图剧中
- android:layout_centerHorizontal
- android:layout_centerVertical
- android:layout_centerInParent
Java代码设置
- RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
- layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
- layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
** Include 与 mege标签
参考资料:
Re-using Layouts with <include/>
Romain Guy Android Layout Tricks #3: Optimize, Part 1
** ViewGroup相关配置
1. clipChildren
android:clipChildren setClipChildren(boolean)Defines whether a child is limited to draw inside of its bounds or not.
定义是否限制子视图在它的范围内进行绘制。默认是true
2. clipToPadding
android:clipToPadding setClipToPadding(boolean)Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area.
定义ViewGroup是否将剪切它的绘制界面并排除padding区域。默认是true
参考资料:
android:layout_gravity
android:layout_gravity="right"
lp.gravity = Gravity.RIGHT;
button.setGravity(Gravity.CENTER);
xml配置:
设置EditText光标位置
http://www.cnblogs.com/nanhuchongzi/archive/2012/04/17/2444451.html
** margin与padding的区别?
view.getLayoutParams().width = widthValue;
http://blog.csdn.net/tu_bingbing/article/details/8654990
**TextView 显示两行,其余用....替换
android:maxLines="2"
android:ellipsize="end"
values/ids.xml
<resources>
<item type="id" name="idname" />
</resources>
view.setId(R.id. idname);
***
布局XML Schema
有哪些属性,是在XML Schema定义,可以在http://www.w3school.com.cn/schema/schema_intro.asp了解Schema更多知识。
Android 布局XML Schema在什么地方? 查询补漏
布局在Java中的对应关系 TypeArray
原文地址:http://blog.csdn.net/love_world_/article/details/8426935
2013-12-29 添加gravity、 layout_gravity、duplicateParentState
2014-04-10 添加布局XML Schema