android布局方式
LinearLayout线性布局
两种布局方式分别是横向和纵向。通过设置android:orientation属性来设置,vertical,horizontal。
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2" />
</LinearLayout>
</LinearLayout>
RelativeLayout相对布局
参考其他元素布局,默认是参考父元素。
属性类型有三类:
1.属性值是true或者false的:
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相对于父元素完全居中。
android:layout_alignParentBottom 位于父元素的下边缘
android:layout_alignParentTop 位于父元素的上边缘
android:layout_alignParentLeft 位于父元素的左边缘
android:layout_alignParentRight 位于父元素的右边缘
2.属性值是其他元素的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 和某元素左方对齐
3.属性值是数值距离的:
android:layout_marginLeft 离某元素左边缘的距离
android:layout_marginRight 离某元素右边缘的距离
android:layout_marginTop 离某元素上边缘的距离
android:layout_marginBottom 离某元素下边缘的距离
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button2"
android:layout_above="@id/button2"
android:text="Button" />
</RelativeLayout>
FrameLayout帧布局
这个布局所有的元素都是从左上角开始的
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:background="#00000000"
android:layout_height="wrap_content" android:text="11111" />
<TextView
android:layout_width="wrap_content"
android:background="#00000000"
android:layout_height="wrap_content" android:text="222222" />
</FrameLayout>
TableLayout表格布局
TableLayout必须配合TableRow使用。
TableLayout包括属性:
android:stretchColumns 设置第几列为伸展(0表示第一列)
android:shrinkColumns 设置第几列为收缩
android:collapseColumns 设置第几列为隐藏
TableRow内的元素包括的属性:
android:layout_column 设置控件在第几列
android:layout_span 设置控件能跨多少列
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="111"></TextView>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="222"></TextView>
</TableRow>
<TableRow>
<TextView android:layout_span="2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="wewwwwwww"></TextView>
</TableRow>
</TableLayout>

浙公网安备 33010602011771号