目录
一个精致的页面通常由很多控件组成,我们用布局管理器来管理组件的分布、大小,而不是直接设置组件的大小、位置。布局是一种可以放置很多组件的容器,当然除了放置组件外,它还可以放置布局,通过多层布局的嵌套,我们就很编写一个复杂精美的界面(Android的布局管理器本身就是一个UI组件,所有的布局管理器都是ViewGroup的子类),下面就让我们看看有哪些常用的布局管理器吧!
一、线性布局(LinearLayout)
1、android:orientation属性
通过属性android:orientation控制线性布局中组件的横向和纵向排列,但线性布局不会自动换行,来实战一下吧!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_3"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button_3"/>
</LinearLayout>
这里需要注意,如果LinearLayout的排列方式是horizontal,那么它的子组件的android:layout_width的属性最好不要是match_parent,因为这样单一个组件就可以把水平方向上的控件全部占满 |
2、android:layout_gravity属性
android:layout_gravity--->用于指定组件在布局中的对齐方式
需要注意的是如果LinearLayout的排列方式为horizontal时(android:orientation="horizontal"),那么只有垂直方向上的对齐方式才会生效,同理,如果LinearLayout的排列方式为vertical,那么只有水平方向上的对齐方式才会生效
3、android:layout_weight属性
这个属性允许我们用比例的形式来控制组件的大小,例如:LinearLayout的排列方式为horizontal时,我们将内部组件的android:layout_width属性的值设为"0dp",再为组件的android:layout_weight属性赋值,决定该组件在水平方向上的宽度占比
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button_1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button_2"/>
</LinearLayout>
如上述代码所示,Button_1在水平方向上的占比为1/3,Button_2在水平方向上的占比为2/3
我们还可以通过控制部分组件的android:layout_weight属性值,来达到更好的页面效果,例如:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Type Something"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send"/>
</LinearLayout>
我们将EditText的android:layout_weight属性值赋值为1;Button的android:layout_width属性值赋值为wrap_content,android:layout_weight属性不赋值;这样Button的宽度仍按wrap_content来计算,而EditText将占满屏幕水平方向上的所有控件,这样方式编写页面,不仅各种屏幕的适配度更好,而且看起来也更加舒服
二、相对布局(RelativeLayout)
相对布局相对于线性布局来说就显得随意一些了,它可以通过相对定位的方式让控件出现在布局的任意位置,也正因为如此,它的属性非常多
1、相对于父布局进行定位
android:layout_alignParentLeft="true" -->让组件的左边缘与父容器的左边缘对齐
android:layout_alignParentRight="true" -->让组件的右边缘与父容器的右边缘对齐
android:layout_alignParentTop="true" -->让组件的上边缘与父容器的上边缘对齐
android:layout_alignParentBottom="true" -->让组件的下边缘与父容器的下边缘对齐
android:layout_centerInParent="true" -->位于父容器的中间
示例:
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button_1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button_2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button_3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Button_4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Button_5"/>
</RelativeLayout>
2、相对于组件进行定位
android:layout_above="组件id" -->位于指定组件的上边
android:layout_below="组件id" -->位于指定组件的下边
android:layout_toRightOf="组件id" -->位于指定组件的右边
android:layout_toLeftOf="组件id" -->位于指定组件的左边
示例:
<?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">
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button_3"/>
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="Button_1"/>
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="Button_2"/>
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toLeftOf="@id/button_3"
android:text="Button_4"/>
<Button
android:id="@+id/button_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button_3"
android:layout_toRightOf="@id/button_3"
android:text="Button_5"/>
</RelativeLayout>
注意,当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后边,不然会出现找不到id的情况,因为我们的button1,button2,button4,button5都是相对于button3布局,都引用了button3的id,所以它们都在button3后进行定义
三、帧布局(FrameLayout)
帧布局的应用场景相对于线性布局和相对布局来说就比较少了,这种布局没有方便的定位方式,所有的控件都会默认摆放在布局的左上角,让我们通过例子来看一下吧
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF00000"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#F00"/>
</FrameLayout>
如上所示,第二个textView会覆盖在第一个textView上,且它们都默认摆放在布局的左上角
不过我们可以通过android:layout_gravity来指定它的对齐方式,这里就不加赘述了
四、表格布局(TableLayout)
1、表格布局的特性
1、表格布局主要通过行、列的形式来管理放入的控件
2、不会明确定义行数与列数,而是通过添加TableRow或者组件的形式来控制行数与列数,添加一个TableRow即添加一个表格行,TableRow也是一个容器,它里面也可以添加组件,每添加一个组件该表格行就增加一列
3、列宽取决于该列中最宽的那个单元格,整个表格布局的宽度取决于父容器的宽度
2、表格布局中单元格的三种行为方式
1、shrinkable:如果该列被设置为shrinkable,则该列的所有单元格宽度可被收缩
对应的XML属性:android:shrinkColumns="列序号"
2、strecthable:如果该列被设置为strecthable,则该列所有单元格宽度均可被拉伸
对应的XML属性:android:strecthColumns="列序号"
3、collasped:如果该列被设置为collasped,则该列的所有单元格会被隐藏
对应的XML属性:android:collapsedColumns="列序号"
注意:1、列序号是从0开始的 2、如果同时指定多列,可用“,”隔开 |