View控件到底是什么,举个例子,一个View控件就是一个省,一个ViewGroup容器就是一个区域,通过各种View控件组合,这就是一个完整UI(User Interface)--中国。
一、线性布局
一个布局就指定了控件中的排列位置,而在线性布局中的控件,被指定为水平或是垂直排列。
常用属性
| 属性名称 | 功能描述 |
|---|---|
| android:orientation | 设置布局内控件的排列顺序 |
| android:layout_weight | 在布局内设置控件权重,属性值可直接使用int值 |
1.android:orientation 用于设置布局内控件的排列顺序
- vertical:表示布局内控件从上到下进行排序
- horizontal:表示布局内控件从左到右进行依次排序
2.android:layout_weight 控件宽度在水平方向上的权重比
示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="按钮3"/>
</LinearLayout>
二、相对布局
相对布局(RelativeLayout),通过相对定位的方式指定子控件的相对位置,可以父容器左上角或其他子控件为参照物。
| 属性名称 | 功能描述 |
|---|---|
| android:layout_centerInParent/Vertical/Horizontal | 设定当前控件位于父布局的中央位置,垂直居中位置,水平居中位置 |
| android:layout_above,below,toleftof,torightof | 相对于某控件的上方、下方、左侧、右侧 |
| android:layout_alignParentTop/Left/Right/Bottom | 当前控件与父控件顶端、左、右、底端对齐 |
| android:layout_alignTop、Bottom、left、Right | 当前控件的上边界和某控件的上边界、下边界、左边界、右边界对齐、 |
| 示例代码: |
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/AppTheme"
android:id="@+id/mainRelativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp"
>
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp"/>
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
<Button
android:id="@+id/btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/btn_two"
android:layout_marginLeft="-5dp"
android:layout_marginBottom="151dp"
android:layout_toRightOf="@id/btn_two"
android:text="按钮3" />
</RelativeLayout>
三、表格布局
表格布局(TableLayout)采用行、列的形式来管理控件,不需要声明表格多少列,只需要通过在表格中添加TableRow布局或控件来控制表格的行数
TableLayout的常用属性
| 属性名称 | 功能描述 |
|---|---|
| android:stretchColumns | 设置可拉伸的列,android:stretchColumns=0时,代表第一列可以拉伸 |
| android:shrinkColumns | 设置可收缩的列,android:stretchColumns=0时,代表第一列可以拉伸 |
| android:collapseColumns | 设置可隐藏的列,android:stretchColumns=0时,代表第一列可以拉伸 |
TableLayout的控件的常用属性
| 属性名称 | 功能描述 |
|---|---|
| android:layout_column | 设置该控件显示的位置,android:layout_column=1,表示在第2个位置显示 |
| android:layout_span | 设置该控件占据的列数 |
示例代码:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮2" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="按钮4" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="按钮5" />
</TableRow>
</TableLayout>
四、帧布局
帧布局(FrameLayout)就是在界面中创建一个空白区域,放置在里面的控件都会一个个叠加,在上面的就会把下面的遮住,没啥好说的,很好理解
| 属性名称 | 功能描述 |
|---|---|
| android:foreground | 设置FrameLayout容器的前景图像 |
| android:foregroundGravity | 设置前景图像显示的位置 |
示例代码如下:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_one"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#ff0000" />
<Button
android:id="@+id/btn_two"
android:layout_width="220dp"
android:layout_height="220dp"
android:layout_gravity="center"
android:background="#00ff00" />
<Button
android:id="@+id/btn_three"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_gravity="center"
android:background="#0000ff" />
<Button
android:id="@+id/btn_four"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:background="#ff1243" />
<Button
android:id="@+id/btn_five"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:background="#324678" />
</FrameLayout>