当前位置:首页 > 生活百科

android相对布局属性(android相对布局讲解)

栏目:生活百科日期:2025-03-01浏览:0

有哪些布局类型?

Android系统中为我们提供的五大布局:LinearLayout(线性布局)、FrameLayout(单帧布局)、AbsoluteLayout(绝对布局)、TablelLayout(表格布局)、RelativeLayout(相对布局)。其中最常用的的是LinearLayout、TablelLayout和RelativeLayout。这些布局都可以嵌套使用。

LinearLayout(线性布局)

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation=&”vertical&” 和 android:orientation=&”horizontal&”来设置。

案例代码分析:

&<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical|center_horizontal" tools:context="zzxb.me.layoutdemo.MainActivity"&> &<Button android:id="@+id/linearLO" android:text="@string/linear_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:id="@+id/tableLO" android:text="@string/table_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:id="@+id/frameLO" android:text="@string/frame_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:id="@+id/relativeLO" android:text="@string/relative_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_weight="4" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:id="@+id/absLO" android:text="@string/abslayout_name" android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&>&</LinearLayout&>

线性布局是用&<LinearLayout&>标签标示,其中常用的属性:

layout_width/layout_height:设置宽度和高度,其值有:wrap_content(适配内容大小),match_parent(适配父容器大小),此两个属性在各个控件中为通用属性

id:唯一标识该控件值

orientation:设置该布局是水平布局(horizontal)还是纵向布局(vertical)

gravity:设置控件的对齐方式,常用值:center_vertical(纵向居中)|center_horizontal(水平居中)

在&<Button&>标签中,也同样有id,layout_width以及lay_height属性。同时,还有如下常用属性:

text:设置按钮文字,这里有两种方式,一种是直接硬编码,即直接写内容,例如:

 android:text="按钮"

第二种方式是非硬编码方式,是通过配置strings.xml文件来配置,例如:

&<resources&> &<string name="btnText"&>按钮&</string&>&</resources&>

然后,通过:

android:text="@string/btnText"

引用。

页面跳转的方式:

 Intent intent = new Intent(); intent.setClass(MainActivity.this,LinearActivity.class); startActivity(intent);

TableLayout(表格布局)

表格布局与常见的表格类似,以行、列的形式来管理放入其中的UI组件。表格布局使用&<TableLayout&>标签定义,在表格布局中,可以添加多个&<TableRow&>标签占用一行。由于&<TableRow&>标签也是容器,所以还可以在该标签中添加其他组件,每添加一个组件,表格就会增加一列。在表格布局中,列可以被隐藏,也可以被设置为伸展的,从而填充可利用的屏幕空间,还可以设置为强制收缩,直到表格匹配屏幕大小。

TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控件就在TableRow之上.TableLayout是一个应用错杂的布局,最简单的用法就仅仅是拖沓控件做出个界面,但实际上,会经常在代码里应用TableLayout,例如做出表格的结果。

重要的几个属性如下:

 android:collapseColumns="1,3" 隐藏第二列和第4列的控件 android:stretchColumns="0,2,4" 第一列和三列以及第五列的空白textview被拉伸 android:shrinkColumns="1,3" 第二列和第4列的控件被收缩

案例代码:

 &<TableLayout android:stretchColumns="0,2,4" android:layout_width="match_parent" android:layout_height="match_parent"&> &<EditText android:hint="请输入用户名" android:textSize="15sp" android:layout_margin="6dp" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/account" android:layout_width="match_parent" android:layout_height="wrap_content" /&> &<EditText android:hint="请输入密码" android:layout_margin="6dp" android:textSize="15sp" android:inputType="textPassword" android:background="@drawable/corner_round" android:drawableLeft="@mipmap/passwowrd" android:layout_width="match_parent" android:layout_height="wrap_content" /&> &<TableRow&> &<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:text="登录" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<Button android:text="注册" android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" /&> &</TableRow&> &</TableLayout&>

FrameLayout(帧布局)

帧布局被设计成在一个屏幕区域显示一个单一的项(single item)。通常FrameLayout显示一个单一的子控件,它支持的布局属性不够丰富,一般通过layout_gravity来设置子控件的位置。

FrameLayout的子控件被绘制在一个堆栈中,最近添加进来的子控件在堆栈的顶部。

案例代码:

 &<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"&> &<ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/movie" android:contentDescription="@string/movie_desc" /&> &<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/button" android:contentDescription="@string/pause_desc" android:layout_gravity="center" /&> &</FrameLayout&>

RelativeLayout(相对布局)

相对布局,子控件的位置关系可以通过子控件与父控件、子控件与子控件来确定,子控件之间位置可以重叠,后面的控件会盖在前面控件之上,拓展性好,灵活方便,是使用最多的布局方式。

案例代码:

 &<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"&> &<EditText android:id="@+id/et_uname" android:hint="请输入用户名" android:textSize="20sp" android:background="@drawable/corner_round" android:layout_alignParentTop="true" android:layout_width="match_parent" android:layout_height="wrap_content" /&> &<EditText android:id="@+id/et_pwd" android:hint="请输入密码" android:inputType="textPassword" android:layout_marginTop="12dp" android:background="@drawable/corner_round" android:textSize="20sp" android:layout_below="@+id/et_uname" android:layout_width="match_parent" android:layout_height="wrap_content" /&> &</RelativeLayout&> &<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"&> &<Button android:id="@+id/btn_login" android:text="登录" android:layout_width="150dp" android:layout_height="wrap_content" /&> &<View android:id="@+id/v1" android:layout_toRightOf="@+id/btn_login" android:layout_width="50dp" android:layout_height="0dp" /&> &<Button android:id="@+id/btn_reg" android:layout_toRightOf="@+id/v1" android:text="注册" android:layout_width="150dp" android:layout_height="wrap_content" /&> &</RelativeLayout&>

相对布局使用&<RelativeLayout&>标签,其常用属性如下:

android:layout_toLeftOf=&”@+id/name&” 指定控件的左边

android:layout_toRightOf=&”@+id/name&” 指定控件的右边

android:layout_above=&”@+id/name&” 指定控件的上边

android:layout_below=&”@+id/name&” 指定控件的下边

ndroid:layout_alignLeft=&”@+id/name&” 与指定控件左对齐

android:layout_alignRight=&”@+id/name&” 与指定控件右对齐

android:layout_alignTop=&”@+id/name&” 与指定控件顶部对齐

android:layout_alignBottom=&”@+id/name&” 与指定控件底部对齐

android:layout_alignParentLeft=&”true&” 与父控件的左边对齐

android:layout_alignParentRight=&”true&” 与父控件的右边对齐

android:layout_alignParentTop=&”true&” 与父控件顶部对齐

android:layout_alignParentBottom=&”true&” 与父控件底部对齐

android:layout_centerHorizontal=&”true&” 在父控件中水平居中

android:layout_centerVertical=&”true&” 在父控件中垂直居中

android_layout_centerInParent=&”true&” 在父控件中中部居中

AbsoluteLayout(绝对布局)

绝对布局,子控件的位置以绝对的位置定位,子控件之间可以重叠,相对于其他布局,缺少灵活性,在最新的android版本中已经不建议使用。

总结

在android布局控制中,最常用的是线性布局和相对布局,往往它们通常是配合使用,也就是嵌套使用。

关于layout_gravity与gravity的区别

从名字上可以看到,android:gravity是对元素本身说的,元素本身的文本显示在什么地方靠着换个属性设置,不过不设置默认是在左侧的。

android:layout_gravity是相对与它的父元素说的,说明元素显示在父元素的什么位置。

比如说button: android:layout_gravity 表示按钮在界面上的位置。 android:gravity表示button上的字在button上的位置。

“android相对布局属性(android相对布局讲解)” 的相关文章

冲牙器哪个牌子好(美国心诺冲牙器VS洁碧冲牙器深度对比

一提到冲牙器,很多人都对这一产品不熟悉。冲牙器早期间在欧洲等发达国家就开始盛行了,世界首款冲牙器就来自美国的知名品牌洁碧,随着科技的不断推进,现在也有越来越多的...

农村妇女在家赚钱项目有哪些,农村妇女挣钱的小窍门

适合妇女赚钱的农村项目有挂面加工、养殖土鸡、小糕点加工、十字绣、旧衣改造等,过去农村主要是男人出去赚钱,妇女在家做饭带小孩,而如今社会妇女也可以做些力所能及的事...

2023年行车记录仪推荐(目前最好的行车记录仪)

在国内市场上,行车记录仪的品牌越来越多,不同牌子的行车记录仪价格差距极大,品质功能有一定的差别。很多车友在选购行车记录仪时不知道该如何选到优良的行车记录仪。今天...

一个手机如何注册微信小号(注册微信小号的实操教程)

最近微信又要搞事情,推出虚拟手机号服务,简称“微小号”,意思就是用户不再需要使用实体手机卡,就可以享受一样的功能。例如接听电话,发送短信等,不需要办理任何业务,...

mac输入法快捷键切换(超详细mac新手教程)

MacBook电脑在切换输入法,只能上下切换输入法。都比较麻烦,要是能一键切换输入法,是不是要方便很多。所以自动切换输入法可以帮我们大大提高工作效率和工作时间。...

mysql数据库实时同步工具(数据库迁移的三个步骤)

我们在开发的时候需要将两台MySQL数据库数据同步,你知道如何实现实现两台MySQL数据库数据的同步吗?想了解的朋友们现在就和56云小编一起去看看具体的实现方法...

创业板退市条件,创业板什么情况下退市

创业板投资风险报告提醒投资者注意防范创业板上市公司的退市风险。由于创业板上市公司平均规模较小,经营也不稳定,因此在具有较大成长潜力的同时也蕴含着较高风险。与主板...

html边框线怎么设置(html网页制作的基本步骤)

CSS3图片边框使用CSS3border-image属性,你可以在元素的周围设置图片边框。一、浏览器支持表中的数字指定完全支持该属性的第一个浏览器版本。数字后面...

丁磊三番五次带货,有道词典笔为何受宠?

直播带货的时代洪流下,CEO带货渐成风潮,董明珠、梁建章、张朝阳等企业掌门人纷纷化身主播,走进直播间,为自家产品高调带货。6月11日,网易CEO丁磊首次在快手开...

dnf最新职业排行,2020dnf职业一览表

六月已经临近尾声了,而国服目前透露出来的职业排名来看,地下城与鬼剑士是真的实锤了,鬼剑士九大职业就霸榜了幻神和一线,本期我们将给大家对这一个排名进行一个简单的分...