Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout

本文详细介绍了Android中的五种布局管理器:RelativeLayout、LinearLayout、FrameLayout、TableLayout和GridLayout,包括它们的常见属性和实践操作,如Relative的软件更新界面实现,LinearLayout的登录和底部栏设计,以及表格布局在计算器实例中的应用。强调了网格布局和表格布局在跨行、跨列及溢出处理上的区别。
摘要由CSDN通过智能技术生成

目录

布局管理器

RelativeLayout

常见属性

Relative的实践操作(实现软件更新界面)

LinearLayout

常见属性

LinearLayout的实践操作(模范登录以及微信底部)

FrameLayout

常用属性

FrameLayout实例

TableLayout

常用属性(继承LinearLayout)

TableLayout实例(计算器·的实例)

GridLayout

常用属性

GridLayout实例

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


布局管理器

Android提供五种布局管理器:

1.相对布局管理器(RelativeLayout):通过相对定位的方式来控制组件的摆放位置。

2.线性布局管理器(LinearLayout):是指在水平或者垂直方向上依次摆放组件。

3.帧布局管理器(FrameLayout):没有任何定位方式,默认情况下,所有的组件都会摆放在容器的左上角,逐个覆盖。

4.表格布局管理器(TableLayout):使用表格的方式按行、列来摆放组件。

5.绝对布局管理器(AbsoluteLayout):通过绝对定位(x,y坐标)的方式来控制组件的摆放位置。(过期)

6.网格布局管理器(GridLayout):通过它可以实现跨行和跨列摆放组件。

Android提供的布局管理器均直接或间接地继承ViewGroup类。

说明:

在Android中,无论是创建哪一种布局都有两种方法,一种是在XML布局文件当中定义,另一种是使用Java代码来创建。推荐使用XML布局文件中定义。

RelativeLayout

常见属性

  1. android:layout_above="@id/xxx" --将控件置于给定ID控件之上

  2. android:layout_below="@id/xxx" --将控件置于给定ID控件之下

  3. android:layout_toLeftOf="@id/xxx" --将控件的右边缘和给定ID控件的左边缘对齐

  4. android:layout_toRightOf="@id/xxx" --将控件的左边缘和给定ID控件的右边缘对齐

  5. android:layout_alignLeft="@id/xxx" --将控件的左边缘和给定ID控件的左边缘对齐

  6. android:layout_alignTop="@id/xxx" --将控件的上边缘和给定ID控件的上边缘对齐

  7. android:layout_alignRight="@id/xxx" --将控件的右边缘和给定ID控件的右边缘对齐

  8. android:layout_alignBottom="@id/xxx" --将控件的底边缘和给定ID控件的底边缘对齐

  9. android:layout_alignParentLeft="true" --将控件的左边缘和父控件的左边缘对齐

  10. android:layout_alignParentTop="true" --将控件的上边缘和父控件的上边缘对齐

  11. android:layout_alignParentRight="true" --将控件的右边缘和父控件的右边缘对齐

  12. android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐

  13. android:layout_centerInParent="true" --将控件置于父控件的中心位置

  14. android:layout_centerHorizontal="true" --将控件置于水平方向的中心位置

  15. android:layout_centerVertical="true" --将控件置于垂直方向的中心位置

 

Relative的实践操作(实现软件更新界面)

<?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:background="@color/design_default_color_secondary_variant"
    tools:context=".MainActivity"
    android:padding="16dp">
​
​
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发现有Widget的新版本,您现在就安装吗?"
        android:id="@+id/text1"
        android:layout_centerInParent="true">
​
    </TextView>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="以后再说"
        android:layout_alignRight="@+id/text1"
        android:layout_below="@+id/text1">
​
    </Button>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="现在更新"
        android:layout_toLeftOf="@+id/button1"
        android:layout_below="@+id/text1">
    </Button>
​
</RelativeLayout>

 

LinearLayout

常见属性

1.layout_gravity 设置布局中控件的位置 top 上 bottom 下 left 左 right 右 center_vertical 垂直居中 center_horizontal 水平居中 center 居中

2.weight(权重)属性

用来设置占布局所占布局得的比重 (1)layout_width都为0时 按照所设置的比重来分配权重 比如:三个控件 1 2 3 weight分比为1、2、3 layout_width = "0dp" 则会将屏幕的大小分为1+2+3=6份 三个控件分别占布局的1/6 2/6 3/6

(2)layout_width都为warp_content时

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值