常用的布局管理器

1.RelativeLayout(相对布局管理器)

必须有一个参考点

(1)

android:gravity

设置各组件的摆放方式

(2)

android:ignoreGravity

设置组件不受前面的组件所影响

1)RelativeLayout.LayoutParams(elativeLayout的内部类)

a)定义:相对布局管理器的组件设置的属性

b)设置组件相对于参考组件的位置的属性

注:上、下、左、右

例:

相对user的下面

c)设置组件与布局管理器哪边对齐的

注:为boolean类型,为true的时候表示对齐

d)设置组件与哪个组件的上下左右边界对齐

e)设置组件位于布局管理器的哪个位置

分别为水平居中的位置、中间位置、垂直居中的位置

例:

例:demo

2.LinearLayout(线型布局管理器)

(1)垂直线性布局管理器

android:orientation="vertical"

注:每一行只能放置一个组件,并且这个组件也不会换行,当组件一个接着一个到边缘之后,剩下的组件不会显示出来

(2)水平线性布局管理器

android:orientation="horizontal"

注:每一列只能放置一个组件,当组件一个接着一个到边缘之后,剩下的组件不会显示出来

(3)定义线性布局管理器:

a)<LinerLayout>标记

android:orientation设置布局管理器的排列方式,=horizontal或vertical
android:gravity设置布局管理器内组件的显示位置的,比如可以设置让组件居中显示或是居右显示,具底显示等等

例:

注:加竖线就可以设置多个属性

例:右下显示:

(4)

子组件的属性 android:layout_weight 设置组件所占的权重,也就是说用于设置组件占父容器剩余空间的比例,默认值是0

解析:把120分配

例:demo1

3.FrameLayout(帧布局管理器)

*<FrameLayout>标记

(1)android:foreground 为帧布局管理器设置一个前景图像

前景图像:始终位于最上层的图像,其他组件是不能盖住的

(2)android:foregroundGravity 设置前景图像的位置

例:

android:foreground="@mipmap/alarm"
android:foregroundGravity="left|top"

例:demo2

4.TableLayout(表格布局管理器)

例:两行六列的表格

例:demo3

<!--每添加一个TableRow标记,这个表格增加一行-->
<!--本身也是一个容器,所以还可以添加其他的组件-->
<!--每添加一个组件,这个组件就占一列-->
<TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        />
</TableRow>

例:设置第二列被隐藏(从0开始),隐藏多个时用','隔开

android:collapseColumns="1"

例:允许被拉伸(第2列)

android:stretchColumns="1"

例:允许被收缩(当一行的内容超出屏幕宽度的时候,将某一列的宽度收缩,让其他的内容显示出来)

android:shrinkColumns="1"

注:也可以不使用TableRow标记,而是添加组件,每添加一个就会增加一行

例:编辑框:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="输入6-16位数字或字母"
    />

5.GridLayout(网格布局管理器)

例:demo4

*网格布局管理器和表格布局管理器的区别:

a.这个单元格的内容不仅可以跨列显示,还可以跨行显示

b.超出容器的组件将会自动换行,表格布局管理器超出的内容不会显示

*<GridLayout>标记

(1)android:columnCount 指定网格的最大列数

(2)android:orientation 指定当没有为放置其中的组件分配行和列的时候,这些组件的排列方式的,设置水平排列horizontal或垂

直排列vertical

(3)android:rowCount 指定网格的最大行数

*内部类:GridLayout.LayoutParams 控制网格布局管理器中各子组件的分布

(1)android:layout_column 指定子组件位于网格的第几列

(2)android:layout_row 指定子组件位于网格的第几行

(3)android:layout_columnSpan 指定子组件横向跨几列

(4)android:layout_rowSpan 指定子组件纵向跨几行

注:需要搭配使用

android:layout_rowSpan="2"
android:layout_gravity="fill"

(5)android:layout_columnWeight 指定子组件在水平方向上的权重的,也就是这个组件分配水平剩余空间的比例的

(6)android:layout_rowWeight 指定子组件在垂直方向上的权重的

(7)android:layout_gravity 设置子组件采用什么方式占据这个网格的空间

6.布局管理器的嵌套

(1)原则:

a.根布局管理器必须包含xmlns属性;

b.在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来;

c.不能嵌套太深,如果嵌套太深,则会影响性能;

7.相关代码

(1)demo

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
​
    tools:context=".MainActivity">
​
    <TextView
        android:id="@+id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/user1"
        android:layout_below="@id/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="明日"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
​
</RelativeLayout>

(2)demo1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:orientation="horizontal"
    tools:context=".MainActivity">
​
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮111111111111111111"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="按钮2"
        />
​
</LinearLayout>

(3)demo2

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@mipmap/alarm"
    android:foregroundGravity="left|top"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
​
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="Hello WorldHello World!"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FFFF00"
        android:text="Hello World!"
        />
​
</FrameLayout>

(4)demo3

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
​
    tools:context=".MainActivity">
    <!--设置第二列被隐藏android:collapseColumns="1"-->
    <!--每添加一个TableRow标记,这个表格增加一行-->
    <!--本身也是一个容器,所以还可以添加其他的组件-->
    <!--每添加一个组件,这个组件就占一列-->
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮311111111111111111111"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮61111111111111111111"
            />
    </TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮61111111111111111111"
        />
</TableLayout>

(5)demo4

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">
​
    <!--指定位于网格的第一列-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_row="0"
        android:text="按钮1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_row="1"
        android:text="按钮2"
        />
    <!--设置跨2行显示-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_row="0"
        android:layout_rowSpan="2"
        android:layout_gravity="fill"
        android:text="按钮3"
        />
​
</GridLayout>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值