简书 android 布局,android常见布局

本文详细介绍了Android中四种常见的布局管理器——线性布局、帧布局、相对布局和网格布局的使用方法及示例。线性布局通过orientation属性实现水平或垂直排列;帧布局所有子视图按顺序叠放;相对布局通过位置属性实现子视图相对位置布局;网格布局则用于创建固定行列的网格系统。示例展示了各种布局在不同场景下的应用。
摘要由CSDN通过智能技术生成

线性布局

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".LayoutActivity">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="第一组"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮2"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮3"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮4"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="第二组"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮1"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_weight="1"

android:text="按钮2"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮3"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮4"/>

效果

3f3199ac7957

image

帧布局

所有的子控件都会在左上角,每个控件都会覆盖前面的控件

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".LayoutActivity">

android:layout_width="200dp"

android:layout_height="200dp"

android:text="我是1"

android:gravity="right|bottom"

android:textColor="#fff"

android:background="#123"/>

android:layout_width="150dp"

android:layout_height="150dp"

android:text="我是2"

android:gravity="right|bottom"

android:textColor="#fff"

android:background="#234"/>

android:layout_width="100dp"

android:layout_height="100dp"

android:text="我是3"

android:gravity="right|bottom"

android:textColor="#fff"

android:background="#456"/>

android:layout_width="50dp"

android:layout_height="50dp"

android:text="我是4"

android:gravity="right|bottom"

android:textColor="#fff"

android:background="#789"/>

效果图

3f3199ac7957

image

相对布局

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".LayoutActivity">

android:id="@+id/tv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="相对布局"

android:textSize="20dp"/>

android:id="@+id/et"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/tv"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="登录"

android:layout_toLeftOf="@+id/btn2"

android:layout_alignTop="@+id/btn2"

android:layout_marginRight="20dp"/>

android:id="@+id/btn2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="注册"

android:layout_below="@+id/et"

android:layout_alignParentRight="true"

android:layout_marginRight="10dp"/>

效果:

3f3199ac7957

image

可能会用到的

随便卡卡

android:layout_toLeftOf

该组件在引用组件的左边

android:layout_toRightOf

该组件在引用组件的右边

android:layout_above

该组件在引用组件的上边

android:layout_below

该组件在引用组件的下边

android:layout_alignParentLeft

对齐父组件的左边

android:layout_alignParentRight

对齐父组件的右边

android:layout_alignParentTop

对齐父组件的上边

android:layout_alignParentBottom

对齐父组件的下边

android:layout_centerInParent

相对于父组件居中

android:layout_centerHorizontal

横向居中

android:layout_centerVertical

垂直居中

表格布局

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:shrinkColumns="1"

tools:context=".LayoutActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮1">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮2">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮3">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮4">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮5">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮6">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮7">

效果图

3f3199ac7957

图片.png

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:stretchColumns="2"

tools:context=".LayoutActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮1">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮2">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮3">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮4">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="按钮5">

效果图

3f3199ac7957

图片.png

压缩和拉伸是按照0,1,2,3...排序的

网格布局

所有子控件默认在GridLayout中横向依次排列,当只等每行的列数时,到达指定列数

会自动换行显示

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:rowCount="6"

android:columnCount="4"

tools:context=".LayoutActivity">

android:text="0"

android:textColor="@android:color/black"

android:textSize="40dp"

android:layout_columnSpan="4"

>

android:text="清除"

android:textColor="@android:color/black"

android:textSize="26dp"

android:layout_columnSpan="4"

>

效果图:

3f3199ac7957

图片.png

可能会用到的

随便卡卡

rowCount

几行

columnCount

几列

layout_rowSpan

合并行的个数

layout_columnSpan

合并列的个数

layout_column

在网格的第几列

layout_row

在网格的第几行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值