首页 > 其他分享 >第二讲 布局

第二讲 布局

时间:2024-03-17 13:55:41浏览次数:15  
标签:layout column text 第二 布局 content wrap android

第二讲 布局

重要:在安卓布局里有且只有一个根布局所以最外层只能有一个Layout但是内部可以嵌套多个类似于树!

在activity_main.xml里面写布局

线性布局

元素按比重划分大小,注意标红的地方不能改

1:1:2划分

线性布局里面的控件只能水平摆或者垂直摆,

看orientation

horizonta水平

vertical垂直

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
>
<Button
android:layout_height="wrap_content"
android:layout_width="0sp"
android:layout_weight="1"
android:text="@string/love"/>
<Button
android:layout_marginLeft="40sp"
android:layout_height="wrap_content"
android:layout_width="0sp"
android:layout_weight="1"
android:text="@string/love"/>
<Button
android:layout_marginLeft="40sp"
android:layout_height="wrap_content"
android:layout_width="0sp"
android:layout_weight="2"
android:text="@string/love"/>

</LinearLayout>

表格布局

1.拉伸列

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:stretchColumns="1,2"可拉伸的列下标,列标是从0开始。

>
<TableRow>
<Button
android:text="1"
android:layout_column="0"
/>

<Button
android:text="2"
android:layout_column="1"
/>
<Button
android:text="3"
android:layout_column="2"
/>
</TableRow>


</TableLayout>
补充说明
1.隐藏列
android:collapseColumns="要隐藏的列标"
例如:隐藏下标为1的列
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="wrap_content"
   android:layout_width="wrap_content"
android:collapseColumns="1"
    >
<TableRow>
    <Button
        android:text="1"
        android:layout_column="0"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
    <Button
        android:text="2"
        android:layout_column="1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
    <Button
        android:text="3"
        android:layout_column="2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        />
</TableRow>
</TableLayout>
2.设定列,当前内容在哪一列
 android:layout_column="列标"

2.收缩列展示

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:collapseColumns="1"
android:shrinkColumns="0"
>
<TableRow>
<Button
android:text="1"
android:layout_column="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:text="2"
android:layout_column="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:text="3"
android:layout_column="2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:text="1"
android:layout_column="0"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:text="2"
android:layout_column="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:text="3"
android:layout_column="2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</TableRow>
</TableLayout> 效果展示

收缩列,多个列逗号隔

android:shrinkColumns="列下标,列下标..." 目的是:收缩指定列以满足当前内容不超过父容器

3.横跨多列

android:layout_span="横跨几列"

注意:横跨必须现有标准才知道怎么合并!

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<TableRow>
<Button
android:text="1"
android:layout_column="0"
/>
<Button
android:text="2"
android:layout_column="1"
/>
<Button
android:text="3"
android:layout_column="2"
/>
</TableRow>
<TableRow>
<Button
android:text="1"
android:layout_column="0"
/>
<Button
android:text="2"
android:layout_span="2"
android:layout_column="1"
/>
</TableRow>
</TableLayout>
注意事项
在表格布局里,这一列的宽度由本列最大宽度定(不算合并列)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
   android:layout_width="match_parent"
    >
<TableRow>
    <Button
        android:text="1"
android:layout_column="0"
android:layout_width="100sp"
        />

    <Button
        android:text="2"
        android:layout_width="100sp"
       android:layout_column="1"
        />
    <Button
        android:text="3"
        android:layout_column="2"
        android:layout_width="200sp"
        />



</TableRow>
<TableRow>
    <Button
        android:text="1"
        android:layout_column="0"
        android:layout_width="100sp"
        />

    <Button
        android:text="2"
        android:layout_width="100sp"
       
        />
    <Button
        android:text="2"
        android:layout_width="100sp"

        />
</TableRow>
</TableLayout>

 

上述代码展示图:


整个表格大小取决于父容器

标签:layout,column,text,第二,布局,content,wrap,android
From: https://www.cnblogs.com/luckyhappyyaoyao/p/18078082

相关文章

  • STM32第九节(中级篇):RCC(第二节)——讲解系统时钟配置函数SetSysClockTo72
    目录前言STM32第九节(中级篇):RCC(第二节)——讲解系统时钟配置函数SetSysClockTo72代码内容位置及检索分析代码 代码展示时钟控制使能闪存控制寄存器配置AHP,APB1,APB2的总线时钟配置锁相环时钟 超频操作小结前言    上节课我们讲了理论部分,那么我们这节课......
  • 关于数据通信知识的补充——第二篇
    目录四.二层交换机5.实现不同vlan通信的原理方法一:路由器网关方法二:单臂路由方法三:三层交换机五.三层路由技术(1)直连路由(2)静态路由(3)动态路由上一篇我们学习了用vlan隔离也可有效优化泛洪,还拉下一个不同vlan间通信的原理,现在我们接着学习。四.二层交换机5.实现不同......
  • 一篇文章带你掌握Flex 布局:语法、实例
    目录前言语法一、Flex布局是什么?二、基本概念三、容器的属性(应用在父元素上)1.**`flex-direction`**:2.**`flex-wrap`**:3.**`flex-flow`**:4.**`justify-content`**:5.**`align-items`**:6.**`align-content`**:四、项目的属性(应用在子元素上)1.**`order`**:2.**`flex-......
  • <爬虫部署,进阶Docker>----第二章 安装Docker
    前言:安装docker---本章是只针对windows的Docker! 如果你需要你就往下看,不需要就换频道~正文:1.安装Docker前配置a.开启虚拟化功能(VT)  -如果你电脑有这个(虚拟化已启用)        直接跳过这一步;如果没有,那你就去对照自己电脑开启虚拟化; 相关链......
  • 【前端】移动端布局
    目录1.移动端特点分辨率二倍图 2.百分比布局3.flex布局 3.1flex布局模型3.2主轴对齐方式 3.3 侧轴对齐方式3.4flex属性 1.移动端特点PC端网页和移动端网页的不同PC端网页:屏幕大,网页固定版心 jd.com移动端网页:屏幕小没有版心网页宽度多数为100% mjd.c......
  • 半导体产业产业链布局杂谈
        参考文献链接https://www.hygon.cn/index ......
  • 天梯选拔赛第二场
    字符串的题目按以前的写法超时了,要时刻学习一下别人优秀的思路和题解前四道简单的模拟题略过基于文化课的算法学习这一题需要注意如下几个点:1.我们要更改的一定要在main和return之外2.是第一个main和最后一个return之间就不符合题意3.从右边开始找使用rfind左边开始找使用fin......
  • Java学习第二天——基础语法
    Java基础语法数据类型强类型语言要求变量的使用要严格符合规定,所有变量都必须先定义后才能使用!!!Java的数据类型分类基本类型(primitivetype)1.数值类型整数类型浮点类型字符类型(只占有两个字节)2.boolean类型:占一位,其值为true或者false引用类型(referencetype)类、接......
  • EI期刊复现:面向配电网韧性提升的移动储能预布局与动态调度策略程序代码!
    适用平台:Matlab+Yalmip+Cplex/Gurobi/Mosek程序提出一种多源协同的两阶段配电网韧性提升策略。在灾前考虑光伏出力不确定性与网络重构,以移动储能配置成本与负荷削减风险成本最小为目标对储能的配置数量与位置进行预布局;在灾后通过多源协同运行与移动储能的动态调度最小化负荷......
  • Bootstrap5(display显示、flex布局相关属性、浮动、定位、文本、栅格系统)
    类中缀的设置技巧1.当多个连续品目使用一个样式时,则给最小的设置即可。比如:大屏以上内边距都是3:p-lh-32.超小屏不设置类中缀的样式比如超小屏内边距时1,小屏内边距时2,中屏及以上内边距是3p-1p-sm-2p-md-3display显示使用display属性,可以改变元素的展示效果.d-none......