Android学习--布局文件解析

本文介绍了Android UI开发中的各种布局方式,包括相对布局、线性布局、表格布局、网格布局、帧布局及绝对布局的设计代码与特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习过Android的基本内容之后开始对Android的ui开发进行学习,主要是对布局的了解和对样式主题的应用。

Android的布局文件是XML的格式,而且该文件在res/layout文件夹中 。而布局文件可以用图形化的视图表示,在Graphical Layout中可以查看到。

Android的布局主要有以下几个类型:相对布局、线性布局、表格布局、网格布局、帧布局、绝对布局这几个方式。不同的布局会有不同的视觉效果。接下来简单学习下各个布局的设计代码:


相对布局的设计代码:(RelativeLayout)

<RelativeLayout xmlns:android="******/apk/res/android"                 //标签

xmlns:tools="******/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity'>


<Button              //控件1

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"                //顶部

android:layout_centerHorizontal="true"            //居中

android:layout_marginTop="260dp"              //设置距离

android:text="Button1"/>


<Button     //控件2

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBottom="@+id/button1"    //对齐

android:layout_marginBottom="100dp"    //距离

android:layout_toRightOf="@+id/button1"   //右边属性

android:text="Button2"/>

</RelativeLayout>

这样就定义了一个相对的布局,代码中的margin用于定义组件间的距离。android:margin用于定义控件四周的边距。而与margin类似的padding则是对内边距的设置,需要注意使用的范围和情况才可。

在你设置控件和布局的时候需要注意使用“match_parent”或者是“wrap_content”的布局格式,需要注意避免将空间的宽度和高度设计为固定的值。


线性布局的设计代码:(LinearLayout)
<?xml version="1.0" encoding="'utf-8"?>

<LinearLayout xmlns:android="******/apk/res/android"                 //标签

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">


<Button               //控件1

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button1"/>


<Button               //控件2

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button2"/>


<Button               //控件3

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button3"/>

</LinearLayout>

线性布局的设计主要分为两种形式:水平线性布局(horizontal)以及垂直线性布局(vertical)。在默认选择的时候是水平显示布局(horizontal)。

表格布局的设计代码:(TableLayout)

<?xml version="1.0" encoding="'utf-8"?>

<TableLayout xmlns:android="******/apk/res/android"                 //标签

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:stretchColumns=“2”>

<TableRow>      //对象

<Button               //控件1

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_column="0"     //第一列

android:text="Button1"/>

<Button               //控件2

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_column="1"

android:text="Button2"/>

</TableRow>

<TableRow>

<Button               //控件3

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_column="1"

android:text="Button3"/>

<Button               //控件4

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_column="1"

android:text="Button4"/>

</TableRow>

<TableRow>

<Button               //控件5

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_column="2"

android:text="Button5"/>

</TableRow>

</TableLayout>
这样的表格布局就形成了,对于表格布局,主要的目的在于将控件排列整齐,按照表格的形式来进行设计,对于表格布局,行数由TableRow来控制,从“0”开始表示第一列。注意的是,表格布局不用设置高度和宽度,宽度一定是match_parent,高度一定为wrap_content。

网格布局的设计代码:(GridLayout)

<?xml version="1.0" encoding="'utf-8"?>

<GridrLayout xmlns:android="******/apk/res/android"                 //标签

android:layout_width="wrap_parent"

android:layout_height="wrap_parent"

android:gravity=“center”

android:columnCount="4"

android:orientation="horizontal">


<Button         

android:layout_column="3"

android:text="/"/>

<Button   android:text="1"/>

<Button   android:text="2"/>

<Button   android:text="3"/>

<Button   android:text="4"/>

<Button   android:text="5"/>

<Button   android:text="6"/>

<Button   android:text="7"/>

<Button   android:text="8"/>

<Button   android:text="9"/>

<Button   android:layout_gravity="fill"

androoid:alyout_rowSpan="3" //占用3行

android:text="+"/>

<Button 

android:layout_columnSpan="2" //占用2列

android:layout_gravity="fill"

android:text="0" />

<button android:text="00"  />

<Button

android:layout_columnSpan="3" //占用3列

android:layout_gravity="fill"

android:text="=" />

</GridLayout>

这个网格布局是Android4.0新增的布局,用一组无限细的直线将绘图区域分成行列和单元。android:layout_column表示按钮在第几列,android:layout_rowSpan表示控件占用几行,android:layout——columnSpan表示控件占几列。


帧布局的设计代码:(FrameLayout)

<?xml version="1.0" encoding="'utf-8"?>

<FrameLayout xmlns:android="******/apk/res/android"                 //标签

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button               //控件1

android:id="@+id/button1"

android:layout_width="294dp"

android:layout_height="294dp"

android:text="Button1"/>

<Button               //控件2

android:id="@+id/button2"

android:layout_width="218dp"

android:layout_height="214dp"

android:text="Button2"/>

<Button               //控件3

android:id="@+id/button3"

android:layout_width="156dp"

android:layout_height="143dp"

android:text="Button3"/>

</FrameLayout>


帧布局的设计是为每一个加入其中的控件创建一个空白的区域,称之为帧。如果添加多个控件则会按照顺序在屏幕的左上角进行显示,并且会透明显示之前的看控件文本。常见的刮刮卡就是通过帧布局来实现的!


绝对布局的设计代码:(AbsoluteLayout)

<?xml version="1.0" encoding="'utf-8"?>

<AbsoluteLayout xmlns:android="******/apk/res/android"                 //标签

android:layout_width="match_parent"

android:layout_height="match_parent">

<Button         

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="50dp"

android:layout_y="50dp"

android:text="Button1"/>

<Button            

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="200dp"

android:layout_y="150dp"

android:text="Button2"/>

</AbsoluteLayout>

绝对布局的设计形式主要在于通过指定x,y的坐标来控制每一个控件的具体位置,实际工程中不提倡这个布局。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值