安卓开发学习5-6:布局管理器:布局管理器嵌套

嵌套布局管理器

很多时候单一的布局管理器很难符合我们的需求,所以需要进行嵌套布局管理器

嵌套原则

  1. 根布局管理器必须包含xmlns属性
  2. 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来﹔
  3. 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来

实例

1、需求:实现如下界面
在这里插入图片描述
2、需求分析:

  1. 整体可以采用线性布局
  2. 对于头像和隔壁的文字两部分可以使用网格布局
  3. 对于网格右边部分可以使用表格布局
  4. 网格右边的底部的日期和回复按钮可以使用相对布局

3、xml文件

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:paddingTop="30dp"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:orientation="vertical"
    >

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        >

        <ImageView
            android:src="@mipmap/t1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            ></ImageView>

        <TableLayout
            android:layout_height="wrap_content"

            android:layout_columnWeight="1"
            android:layout_width="0dp"

            android:layout_marginLeft="10dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1号"
                >

            </TextView>

            <TextView
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="fill_horizontal"
                android:text="在新冠病毒的攻势前,美国又溃败了。“奥密克戎”的传播越来越快、死亡人数也正在增加;据美国约翰·霍普金斯大学统计数据,截至2022年1月8日上午6时,美国累计确证病例接近6000万、死亡病例约83.6万例;在过去24小时里新增82.2万例、新增死亡2437例。

这些数字已经让人麻木,美国媒体已经开始一本正经地讨论如何“与病毒达成新的共存”。但是拜登似乎很“支棱”,他在接受媒体采访时坚称,美国不准备与病毒永久共存,他不认为这种病毒会长期存在;与此同时,白宫发言人普萨基也在记者会上坚称“美国疫情没有失控”。

这种说法,在外界看来纯粹是掩耳盗铃。前些日子,拜登承认自己没有一整套应对疫情的策略,并且宣布将抵抗疫情的责任下放到州一级,也就是各州“各自为战、自求多福”,等于是“承认战败”;随后联邦层面放松了疫情管控措施,这大概也是最近疫情起飞的重要原因之一,把民众交给病毒宰割又等于“割地赔款”;疫情影响下,经济恢复、社会恢复常态又遥遥无期——无论从哪个角度看,美国已经输得一塌糊涂。"
                >
            </TextView>

            <RelativeLayout

                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="昨天"

                    >
                </TextView>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/reply"
                    android:layout_alignParentRight="true"
                    >
                </ImageView>
            </RelativeLayout>

        </TableLayout>
    </GridLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="2"
        >
        <ImageView
            android:src="@mipmap/t2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            ></ImageView>
        <TableLayout
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:layout_width="0dp"
            android:layout_marginLeft="10dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2号"
                >
            </TextView>

            <TextView
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="fill_horizontal"
                android:text="这里是一些日常"
                >
            </TextView>

            <RelativeLayout
                android:id="@+id/footer_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="1分钟前"
                    >
                </TextView>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/reply"
                    android:layout_alignParentRight="true"
                    >
                </ImageView>
            </RelativeLayout>

        </TableLayout>

    </GridLayout>
</LinearLayout>

4、资源
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值