布局里的普通属性:
layout_width 定义主件相对父容器的宽度
layout_height 定义主件相对父容器的高度
match_parent 充满父容器
wrap_content 完全包裹自己的内容
android:text 显示文本内容
TextView以及子类才有显示文本内容的功能
android:orientation定义主件的排列的方式
horizontal 水平排列
vertica 垂直排列
1)linearLayout线性布局
默认的排列方式是水平排列方式
<LinearLayout
android:orientation="vertical"垂直
android:gravity="center">
<TextView
android:layout_weight="1"> 这个属性按照权重来瓜分布局
共同的属性:
android:gravity设置组件的内容对齐方式 如按钮里的文本
android:layout_gravity="center"设置组件在父容器中的对齐方式
android:layout_width="wrap_content"相对于父容器
android:layout_height="wrap_content"
2)RelativeLayout:相对布局
相对布局容器内子组件的位置总是相对兄弟组件,父容器来决定的。
如果没有设置属性相对布局和单帧布局是类似的。
代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerInParent="true"
android:id="@+id/center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_toLeftOf="@id/center"
android:layout_alignTop="@id/center"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_toRightOf="@id/center"
android:layout_alignTop="@id/center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#d22422"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="回退"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:layout_centerInParent="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="发送" />
</RelativeLayout>
</RelativeLayout>
效果如图:
3)TableLayout:表格布局
TableRow来表示一行
android:collapseColumns: 隐藏某列
android:stretchColumns:扩展某列,为了充满父容器的行
值是数字,从0开始,如果有多个数字用逗号隔开
4)单帧布局 FrameLayout 只有一个位置可以显示内容.
单帧布局的所有组件都放在容器的左上角。
霓虹灯的效果
5)绝对布局 AbsoluteLayout
这种布局现在已经丢弃了,不怎么用