线性布局(LinearLayout)
视图的排列方式
使用 orientation 属性值决定内部视图的排列方式(horizontal 为水平,vertical 为垂直)
如果不指定排列,则默认为水平排列
1 2 3 4 5 6 7 8 9 10 11
| <?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="300dp" android:background="@color/teal_200" android:orientation="horizontal">
</LinearLayout>
|
视图的权重
权重使用 layout_weight 参数设置,指的是布局中,下级视图各自拥有宽高的比例。
这个参数在该视图的下级视图中设置,表示下级视图占据的宽高比例。
- 当设置子布局的宽为0时,权重就表示占据宽度的比例
- 当设置子布局的高为0时,权重就表示占据高度的比例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <LinearLayout android:layout_width="0dp" android:layout_height="200dp android:layout_margin="10dp" android:layout_weight="3" android:background="#3388FF" android:padding="10dp"/>
<LinearLayout android:layout_width="0dp" android:layout_height="200dp android:layout_margin="10dp" android:layout_weight="1" android:background="#3388FF" android:padding="10dp"/>
|
上面的代码中,子部件1权重为3,子部件2权重为1,那么子部件1就占据宽度的3/4,而子部件2占据宽度的1/4
相对布局(RelativeLayout)
相对布局中,视图的位置由其他视图决定,决定视图排列的属性及其含义如下表所示,默认情况下视图从左到右,从上到下排列:
| 相对位置的属性取值 |
相对位置说明 |
| layout_toLeftOf |
当前视图在指定视图的左边 |
| layout_toRightOf |
当前视图在指定视图的右边 |
| layout_above |
当前视图在指定视图的上方 |
| layout_below |
当前视图在指定视图的下方 |
| layout_alignLeft |
当前视图与指定视图的左侧对齐 |
| layout_alignRight |
当前视图与指定视图的右侧对齐 |
| layout_alignTop |
当前视图与指定视图的顶部对齐 |
| layout_alignBottom |
当前视图与指定视图的底部对齐 |
| layout_centerInParent |
当前视图在上级视图中间 |
| layout_centerHorizontal |
当前视图在上级视图的水平方向居中 |
| layout_centerVertical |
当前视图在上级视图的垂直方向居中 |
| layout_alignParentLeft |
当前视图与上级视图的左侧对齐 |
| layout_alignParentRight |
当前视图与上级视图的右侧对齐 |
| layout_alignParentTop |
当前视图与上级视图的顶部对齐 |
| layout_alignParentBottom |
当前视图与上级视图的底部对齐 |
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="300dp" android:orientation="horizontal" tools:context=".MainActivity">
<TextView android:id="@+id/in_the_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000" android:text="正中心" android:textColor="#FFFFFF" android:textSize="11sp" android:layout_centerInParent="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000" android:text="在中心视图的上方" android:textColor="#FFFFFF" android:textSize="11sp" android:layout_above="@id/in_the_center"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000" android:text="底部对齐" android:textColor="#FFFFFF" android:textSize="11sp" android:layout_alignParentBottom="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#000000" android:text="与中心视图的左侧对齐" android:textColor="#FFFFFF" android:textSize="11sp" android:layout_alignRight="@id/in_the_center"/>
</RelativeLayout>
|
网格布局(GridLayout)
网格布局支持多行多列的表格式排列,通过 columnCount 属性和 rowCount 属性分别指定网格的列数和行数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="300dp" android:columnCount="2" android:rowCount="3" tools:context=".MainActivity">
</GridLayout>
|
然后在子视图中,使用 layout_rowWeight 和 layout_columnWeight 属性来指定该视图占据上级试图的(行或列)网格数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <TextView android:layout_rowWeight="1" android:layout_columnWeight="1" android:gravity="center" android:text="第1个网格"/> <TextView android:layout_rowWeight="1" android:layout_columnWeight="1" android:gravity="center" android:text="第2个网格"/> <TextView android:layout_rowWeight="1" android:layout_columnWeight="1" android:gravity="center" android:text="第3个网格"/> <TextView android:layout_rowWeight="1" android:layout_columnWeight="1" android:gravity="center" android:text="第4个网格"/>
|
滚动视图有两种:
- ScrollView:垂直方向滚动,此时 layout_width 的值为 match_parent, layout_height 的值为 wrap_content
- HorizontalScrollView:水平方向滚动,layout_width 和 layout_height 的值与垂直方向时相反
以水平方向的滚动视图为例,垂直方向同理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">
<HorizontalScrollView android:layout_width="wrap_content" android:layout_height="200dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal">
<View android:layout_width="300dp" android:layout_height="match_parent" android:background="@color/teal_700" />
<View android:layout_width="340dp" android:layout_height="match_parent" android:background="@color/purple_200"/>
</LinearLayout> </HorizontalScrollView>
</LinearLayout>
|