- 嵌套布局管理器
- 嵌套原则
- 实例
嵌套布局管理器
很多时候单一的布局管理器很难符合我们的需求,所以需要进行嵌套布局管理器
嵌套原则
- 根布局管理器必须包含xmlns属性
- 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来﹔
- 在一个布局文件中,最多只能有一个根布局管理器,如果需要有多个还需要使用一个根布局管理器将它们括起来
实例
1、需求:实现如下界面
2、需求分析:
- 整体可以采用线性布局
- 对于头像和隔壁的文字两部分可以使用网格布局
- 对于网格右边部分可以使用表格布局
- 网格右边的底部的日期和回复按钮可以使用相对布局
3、xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android/apk/res/android"
xmlns:app="http://schemas.android/apk/res-auto"
xmlns:tools="http://schemas.android/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、资源
更多推荐
安卓开发学习5-6:布局管理器:布局管理器嵌套
发布评论