1、线性布局
LinearLayout:控制子控件按照横向或纵向排列
1. 灵魂属性:android:orientation=”vertical”;(horizontal)
2. gravity属性:控制布局内所有子控件在父容器中的相对位置
-center 水平垂直居中(在中央)
-center_horizontal 水平居中
-center_vertical 垂直居中
-top 顶部
-bottom 底部
-left 靠左
-right 靠右
2、相对布局
RelativeLayout:子控件灵活的出现在某特殊的位置上;方便描述控件与控件的位置
1. 根据父容器定位
2. 根据兄弟控件定位
3. margin(偏移):设置组件与父容器之间的边距
4. padding(填充):控件内部元素间的边距(比如TextView内部字体的位置)
3、帧布局
FrameLayout:子控件之间是层叠覆盖的关系;多应用在滑动页和碎片出现的场合
1. android:foreground: 设置次帧布局容器的前景图像
2. android:foregroundGravity: 设置前景图像显示的位置
4、表格布局
TableLayout:确定表格的行数;设置某一行中某列元素 隐藏 拉伸 或 收缩
1. android:collapseColumns:设置需要被隐藏的列的序号
2. android:shrinkColumns:设置允许被收缩的列的列序号
3. android:stretchColumns:设置运行被拉伸的列的列序号
5、网格布局
GridLayout
1. 设置排列方式
2. 设置几行几列
android:rowCount;网格布局中的行数
android:columnCount; 网格布局中的列数
3. 设置组件所在的行列
4. 设置组件横跨几行几列
关键点一、android:gravity vs android:layout_gravity
- android:gravity设置布局内部所以的子View
- android:layout_gravity设置View本身
- 在线性布局中android:layout_gravity属性的设置与父布局的
android:orientation有关 - 当 android:orientation=”vertical” 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。 即:left,right,center_horizontal 是生效的。 当 android:orientation=”horizontal” 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。 即:top,bottom,center_vertical 是生效的
关键点二、layout_weight权重属性
- 在线性布局中:若布局灵魂属性为横向,在子控件中将(宽)
设置为0dp(android:layout_width=”0dp”),相对最外层布局则会按照权重属性分配剩余空间。纵向排列同理。 - 根据orientation属性,比如是横向的,在设置View宽度属性为包裹内容,即android:layout_width=”wrap_content” ,则相对最外层布局则会按照权重属性分配剩余空间,借鉴代码,加强理解。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="one"
android:background="#98FB98"
/>
<TextView
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="two"
android:background="#FFFF00"
/>
<TextView
android:layout_weight="3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="three"
android:background="#FF00FF"
/>
</LinearLayout>
3.如果设置
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” 属性,则需要重新计算,计算依据为权重每次分的是父容器的剩余空间(不论是纵向还是横向)
关键点三、为LinearLayout设置分割线
- 直接添加一条线的View,代码如下
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
2.LinearLayout的一个divider属性