Android布局
线性布局和相对布局用的比较多一点,其他布局用的少一点就不详写了
1. 相对布局
有三种类型的属性:
属性值是true或false
- android:layout_centerHrizontal 水平居中
- android:layout_centerVertical 垂直居中
- android:layout_centerInparent 相对于父元素完全居中。
- android:layout_alignParentBottom 位于父元素的下边缘
- android:layout_alignParentTop 位于父元素的上边缘
- android:layout_alignParentLeft 位于父元素的左边缘
- android:layout_alignParentRight 位于父元素的右边缘
属性值是”@id/*“
- android:layout_below 在某元素的下方
- android:layout_above 在某元素的上方
- andorid:layout_toRightOf 在某元素的右方
- android:layout_toLeftOf 在某元素的左方
- android:layout_alignBottom 和某元素下方对齐
- android:layout_alignTop 和某元素上方对齐
- android:layout_alignRight 和某元素右方对齐
- android:layout_alignLeft 和某元素左方对齐
属性值是数值
- android:layout_marginLeft 离某元素左边缘的距离
- android:layout_marginRight 离某元素右边缘的距离
- android:layout_marginTop 离某元素上边缘的距离
- android:layout_marginBottom 离某元素下边缘的距离
<?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="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:text="hello"
android:layout_weight="1"
android:textColor="@color/colorPrimary"
/>
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:text="Android"
android:layout_weight="1"
android:textSize="30sp"
android:textColor="@color/colorAccent"
/>
</RelativeLayout>
效果图:
2. 线性布局(有水平和垂直的布局方式,可以设置权重)
- android:orientation=”horizontal” 水平
- android:orientation=”vertical” 垂直
- android:layout_weight=”1” 设置权重
- android:layout_gravity ( 是本元素相对于父元素的对齐方式 )
- android:gravity=”bottom|right”(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.qianfeng.demo1.MainActivity"
android:weightSum="6" //总的权重为6
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"//此控件占用比例为2,其他控件全部占用1,超过比例的权重都不会分配对应的大小,相当于大小为0
android:textSize="30sp"
android:text="aaaa"
android:background="#f00"
p
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="30sp"
android:text="bbbb"
android:background="#0f0"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="30sp"
android:background="#00f"
android:text="cccc"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="30sp"
android:text="dddd"
android:background="#0ff"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="30sp"
android:background="#f0f"
android:text="eeee"
/>
</LinearLayout>
效果图:
3. 绝对布局
定义两个控件左上角坐标轴 ,即android:layout_x 和android:layout_y来控制位置。
4. 表格布局
写在TableLayout中的属性
- android:stretchColumns 设置第几列为伸展(0表示第一列)
- android:shrinkColumns 设置第几列为收缩
- android:collapseColumns 设置第几列为隐藏
写在TableRow里的控件里的属性
- android:layout_column 设置控件在第几列
- android:layout_span 设置控件能跨多少列
<TableLayout>
<TableRow>
<!-在这里填充第一行的元素->
</TableRow>
<TableRow>
<!-在这里填充第二行的元素->
</TableRow>
</TableLayout>
5. 帧布局
是从左上角开始,后面的会覆盖前面的控件
6. 网格布局
本身属性
- android:alignmentMode
说明:当设置alignMargins,使视图的外边界之间进行校准。可以取以下值:
alignBounds – 对齐子视图边界。
alignMargins – 对齐子视距内容。 - android:columnCount
说明:GridLayout的最大列数 - android:rowCount
说明:GridLayout的最大行数 - android:columnOrderPreserved
说明:当设置为true,使列边界显示的顺序和列索引的顺序相同。默认是true。 - android:orientation
说明:GridLayout中子元素的布局方向。有以下取值:
horizontal – 水平布局。
vertical – 竖直布局。 - android:rowOrderPreserved
说明:当设置为true,使行边界显示的顺序和行索引的顺序相同。默认是true。
android:useDefaultMargins
说明: 当设置ture,当没有指定视图的布局参数时,告诉GridLayout使用默认的边距。默认值是false。
子元素属性
- android:layout_column
说明:显示该子控件的列,例如android:layout_column=”0”,表示当前子控件显示在第1列,android:layout_column=”1”,表示当前子控件显示在第2列。 - android:layout_columnSpan
说明:该控件所占的列数,例如android:layout_columnSpan=”2”,表示当前子控件占两列。 - android:layout_row
说明:显示该子控件的行,例如android:layout_row=”0”,表示当前子控件显示在第1行,android:layout_row=”1”,表示当前子控件显示在第2行。 - android:layout_rowSpan
说明:该控件所占的列数,例如android:layout_rowSpan=”2”,表示当前子控件占两行。 - android:layout_columnWeight
说明:该控件的列权重,与android:layout_weight类似,例如有GridLayout上两列,都设置android:layout_columnWeight = “1”,则两列各占GridLayout宽度的一半 - android:layout_rowWeight
说明:该控件的行权重,原理同android:layout_columnWeight。