Android新手教学:淘宝UI常用布局-LinearLayout(线性布局)
yuyutoo 2024-10-19 11:07 5 浏览 0 评论
LinearLayout(线性布局)
Android五大布局
本篇开始介绍Android的五大布局的知识,一个丰富的界面显示总是要有众多的控件来组成的,那么怎样才能让这些控件能够按你的想法进行摆放,从而自定义你所想要的用户界面呢?这就牵涉到本章将要学习的知识————五大布局。本篇将依次对LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)、GridLayout(网格布局)进行介绍。
LinearLayout(线性布局)
这是一个非常常用的布局,它会将其中的控件在线性方向上依次排列,通过android:orientation属性指定其控件的排列方向,有vertical(垂直方向)以及horizontal(水平方向)排列。新建UILayoutTsetOne项目,其他设置保持默认。修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
接下来将vertical参数改变为horizontal参数。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
运行程序,效果如下,从图中可以看出,定义的三个button组件按照horizontal依次排列。
attention!倘若LinearLayout的排列方向指定为horizontal,则内部的控件就绝对不能将宽度指定为match_parent,因为如果这样设置,单独的控件将会将整个水平方向占满,其他控件将没有放置的位置了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
效果如图:
同样,倘若LinearLayout的排列方向指定为vertical,则内部的控件就绝对不能将高度指定为match_parent。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
效果如图:
下面来看两个长得很像的属性:android:gravity属性和android:layout_gravity属性。
- android:gravity属性:用于指定文字在控件中的对齐方式。可以选择的值有:top、bottom、left、right、center等,还可以用“|”来同时指定多个值,其中center值将相当于center_vertical|center_horizontal,表示文字在垂直和水平方向都居中对齐。
- android:layout_gravity属性:用于指定控件在布局中的对齐方式。其可选值和android:gravity属性差不多,需要注意的是,当LinearLayout的排列方向是horizontal时只有垂直方向上的对齐方式才会生效,因为此时水平方向上的长度是不固定的,每添加一个控件,水平方向上的长度都会改变,因而无法指定该方向上的对齐方式。同样,当LinearLayout的排列方向是vertical时,只有水平方向上的对齐方式才会生效。修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Button 3" />
</LinearLayout>
运行效果如图:
接下来,我们学习另一个重要属性:android:layout_weight,它允许我们使用比例的方式来指定控件的大小,在手机的适配性方面可以起到非常重要的作用。这里通过编写一个消息发送界面来做演示。所用到的控件有:一个文本编辑框和一个发送按钮。修改activity_main.xml中的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/input_msg"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type in Some words" />
<Button
android:id="@+id/send_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send_msg" />
</LinearLayout>
运行程序,效果如图:
这里你会发现EditText和Button的宽度都被指定为了0dp,你可能会担心这样这两个控件还能正常地显示出来吗?不用担心,因为这里,使用了android:layout_weight属性,此时控件的宽度就不由android:layout_width来决定了,这里写成了0dp是一种比较标准的写法。另外,dp是Android中用于指定控件大小、间距等属性的单位。可以看到这里通过android:layout_weight属性将值指定为了1,这表示两个控件在水平方向上平分宽度。原理:系统会将所有控件指定的layout_weight值相加,得到一个总值,然后每个控件所占大小的比例就是用该控件指定的layout_weight值除以刚才算出的总值。因此如果想让EditText占据屏幕宽度的3/5,Button占据屏幕宽度的2/5,只需要将EditText的layout_weight改成3,Button的layout_weight改成2就可以了。重新运行程序,效果如图:
接着再来看一下如何实现在两个控件之间用分割线进行分割,效果如图:
实现这种效果有两种方式:
- 1.直接在布局中添加一个view,这个view的作用仅仅是显示出一条线,实现如下:
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 1" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 2" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button 3" />
</LinearLayout>
2.使用LinearLayout的一个divider属性,直接为LinearLayout设置分割线,这里需要准备一张线的图片 1)android:divider设置作为分割线的图片 2)android:showDividers设置分割线的位置,none(无),beginning(开始),end(结束),middle(每两个组件间) 3)dividerPadding设置分割线的Padding
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/thread"
android:orientation="vertical"
android:showDividers="middle"
android:dividerPadding="10dp"
tools:context="com.example.uilayouttestone.MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
以上就是线性布局LinearLayout布局的详解,有问题可以关注公共号随时提问
相关推荐
- 从头再来,再见了我曾奋斗的地方
-
今天,我告别了奋斗二十年的地,就现在也不敢相信曾经是那么的辉煌那么让我骄傲的厂子今天确是也要告别了,我们不知道该怨谁该谁买单可苦的是我们还得从头再来...
- 《万古神帝》第4202章(评)咬定神树不放松,第二儒祖仍在破局中
-
各位老铁,酒窝突然有了一个匪夷所思的猜想,从突袭天庭,再到夺取世界树,到天姥和若尘打上天界对战第二儒祖和慕容主宰。这一切会不会只是时空人祖一开始就谋划好的,引蛇出洞之计?第二儒祖乃至整个神界也只不过是...
- 香皂和透明皂可以混用吗?
-
香皂和透明皂是家庭常用物品,使用频率比较高。但是,有些市民认为香皂比较柔和,香味也比较好,因此喜欢用香皂给孩子洗衣服。而有些成年人,特别是男性,觉得透明皂的去污能力强,有时候会使用透明皂洗手或洗澡。那...
- 批评别人的技巧
-
批评和赞美是一对反义词,但在说话这门艺术里,两者不过是两种与人交流沟通的不同方式而已,赞美侧重于一个人的优点和长处,批评侧重于一个人的缺点和不足。赞美人需要讲究方式和技巧,批评人同样不能草率从事。批评...
- 山东2020春季高考技能考试主考学校及考试安排来了
-
山东2020年春季高考技能考试安排在7月14日—20日进行,各类别具体考试时间由主考学校自主确定。考生请于7月11日起登录山东省教育招生考试院招生考试信息平台(http://wsbm.sdzk.cn)...
- 石河子大学2019-2020学年第二学期选课通知
-
它来了,它来了小石带着秘籍来了!又快到了每学期的选课时间内心是不是既兴奋又紧张敲黑板,划重点!为保证选课工作的顺利进行现将选课事宜安排如下你想要的课程安排选课时间及其相关要求...
- “奋进新时代”主题成就展线上开展!一起为山西单元云端打call
-
登录网址,点击按钮,“祝融号”火星车模型、天问一号着陆器模型、东风-17高超音速导弹等实物展品立刻呈现;跟随着语音讲解,奋进山西单元的精彩内容就跃然眼前……近日,“奋进新时代”主题成就展网上展馆上线。...
- 延边大学关于2021年秋季学期本(专)科教学运行工作安排的通知
-
关于2021年秋季学期本(专)科教学运行工作安排的通知根据学校《关于2021年秋季学期师生返校安排的通知》和《关于2021级新生报到相关事宜的通知》,为做好2021年秋季学期开学期间的本(专)科教学相...
- 阿里巴巴Yun OS智能手表Pay Watch亮相:深度整合支付宝
-
IT之家讯9月22日消息,刚刚我们报道了阿里巴巴智能手表亮相YunOS官网,本来以为阿里巴巴会重点介绍一下这款产品。没想到这款智能手表已经悄悄的登录淘宝众筹了。与此同时,它的名字也正式揭晓:Pay...
- 内网本机IP怎么查?外网本机IP地址查询方法
-
在工作中,我们或许要查看本机的ip地址,不是很懂电脑的人,对此可能会无从下手,今天飞飞就给你们分享外网/内网本机IP地址查询方法,感兴趣的往下看,非常简单,肯定可以学会...
- 查完成绩,你是不是还需要了解一下这个「奸笑脸」
-
各位假期生活过得如何?不管你是在各个城市享受旅行,或是已经摊在家里过着肥宅的快乐生活。都醒一醒吧!这一次不同于各种校园推送里的假消息是真的可以查成绩了!成绩已于今日上午...
- 「鼠标」垂直鼠标再体验:多彩DELUX 90度垂直鼠 M618与M618PLUS
-
我因为用原来的鼠标时间比较久了,想换个姿势用鼠标。我仔细搜了一下,垂直鼠标,只找到罗技的MXVERTICAL和多彩的M618系列产品。因为我的初体验是罗技的MXVERTICAL,所以这篇叫“垂直...
- 说一个目前最让你焦虑的事?
-
来说说那些让你感到非常焦虑但是不被生活中你的亲人朋友所知道的吧,在这里说一下,写下来,聊一聊,互相温暖鼓励一下吧!...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)