Android 第六课 4种基本布局之LinearLayout和Relativelayout

看完控件,紧接着看布局,布局是可以来放置控件,管理控件的。布局里也可以嵌套布局。


我们新建项目UILayoutTest项目,活动名和布局名选择默认。加入活动及其对应的布局已经创建完成。

  • 线性布局(LinearLayout)

  • android:layout_gravity属性
  • android:layout_weight属性

  • 相对布局(RelativeLayout)

  • 相对于父布局定位
  • 相对于控件定位:注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id的情况

        LinearLayout布局会将它所包含的控件在线性方向上一次排列,所谓线性方向,可能是垂直方向,或者是水平方向,前面我们都是在垂直方向上排列控件,我们可以通过android:orientation属性指定了排列方向是vertical,如果指定的是horizontal,控件就会在水平方向上排列了。修改activity_layout.xml新增加三个按钮,我们可以想不写android:orientation属性,发现3个按钮根据自身大小水平排列,因为这是LinearLayout布局默认的控件排列方式,(倘若你的第一个控件的android:layout_width="match_parent",就是说你的一个控件的宽度等于整个屏幕的宽度,那么你后面的哪个控件很有可能显示不出来。)

等到我们写过android:orientation="vertical",我们会发现3个按钮垂直的排列了。

        

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>

这里需要注意:如果LinearLayout的排列方式是horizontal,内部的控件就不能将宽度指定为match_parent,因为这样,单独一个控件会将整个水平方向占满,其他控件就没有可以放置的位置了。同样的道理如果LinearLayout的排列方式是horizontal,内部控件就不能将高度指定为match_parent,因为这样,单独一个控件会将整个垂直方向占满。

我们首先来看布局的android:layout_gravity属性,我们之前看过android:gravity属性,这两者有些相似。区别是:android:gravity是指定文字在控件中的对齐方式,而android:layout_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"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top"android:text="Button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_vertical"android:text="Button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:text="Button 3"android:textAllCaps="false"/></LinearLayout>

我们再来看LineraLayout中另一个重要属性android:layout_weight。它允许我们使用比例的方式来指定控件的大小,它可以去适应手机的屏幕。下面我们来编辑一个消息发现界面,需要一个文本编辑框和一个发送按钮,修改activity_main.xml中的代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="3"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="0dp"android:layout_height="wrap_content" android:layout_weight="2"android:text="Send"/></LinearLayout>

发现<EditText>和<Button>的宽度都指定为了0dp,不过你不用担心文本编辑框和按钮不存在,因为我们又接着使用了android:layout_weight属性,此时控件大小由android:layout_weight来决定。这里0dp是一种比较规范的写法。另外,dp是Android中用于指定控件大小、间距等属性的单位。在这里我们把<EditText>和<Button>的android:layou_weight分别设置为3和2,这表明EditText占整个屏幕宽度的3份,Button占2份(我们是将整个屏幕分为3+2份)。

当然,我们也可以只是指定部分控件的layout_weight值来实现更好的效果。修改activity_main.xml文件中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><EditTextandroid:id="@+id/input_message"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="3"android:hint="Type something"/><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Send"/></LinearLayout>

我们只是指定了EditText的android:layout_weight属性,并将Button的宽度改为wrap_content。这表明Button的宽度仍然按照wrap_content来计算,而EditText则会占满屏幕的所有剩余空间。




  • 相对布局(RelativeLayout)
  • android:layout_alignParentLeft属性可以让控件和父布局的左上角对齐
  • android:layout_above属性可以让一个控件位于另一个控件的上方
  • android:layout_alignLeft表示让一个控件的左边缘和另一个控件的左边缘对齐

相比较LinearLayout,RelativeLayout更加随意一些,它通过相对定位的方式让控件出现在布局的任何位置。正因为如此,RelativeLayout的属性非常多,不过都是有规律可循的。修改activity_main.xml中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentBottom="true"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:text="button 5"/></RelativeLayout>

我们让Button 1和父布局的左上角对齐,Button 2和父布局的右上角对齐,Button 3居中显示,Button 4 和父布局的左下角对齐,Button 5 和父布局的右下角对齐。程序运行如下:



上面这是相对于父布局定位。

下面我们相对于控件进行定位。

修改activity_main.xml中的代码,如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_above="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 1"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 2"/><Buttonandroid:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="button 3"/><Buttonandroid:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toLeftOf="@id/button3"android:text="button 4"/><Buttonandroid:id="@+id/button5"android:textAllCaps="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button3"android:layout_toRightOf="@id/button3"android:text="button 5"/></RelativeLayout>

其中android:layout_above属性可以让一个控件位于另一个控件的上方,需要为这个属性指定相对控件的id的引用。

android:layout_toLeftOf表示一个控件位于另一个控件的左侧。注意:当一个控件去引用另一个控件的id时,该控件一定要定义在引用控件的后面,不然会找不到id的情况。重新运行程序,如下图:


RelativeLayout中还有另外一组相对于控件进行定位的属性,android:layout_alignLeft表示让一个控件的左边缘和另一个控件的左边缘对齐,android:layout_alignRight、android:layout_alignTop、android:layout_alignBottom道理是一样的。



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/274125.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何在UI设计中制作完美阴影

重点 (Top highlight)Shadows are everywhere in modern UI Designs. They are one of the most essential part of the UI elements right behind the fill, stroke, and cornder radius. &#x1f609;现代UI设计中到处都有阴影。 它们是UI元素中最重要的部分之一&#xff0c…

微软2013年校园实习生招聘笔试题及答案

原文&#xff1a; http://www.wangkaimin.com/2013/04/07/%e5%be%ae%e8%bd%af2013%e5%b9%b4%e6%a0%a1%e5%9b%ad%e5%ae%9e%e4%b9%a0%e7%94%9f%e6%8b%9b%e8%81%98%e7%ac%94%e8%af%95%e9%a2%98%e5%8f%8a%e7%ad%94%e6%a1%88/#more-195 1. Which of following calling convension(s)…

Android 第七课 4种基本布局之FrameLayout和百分比布局

FrameLayout&#xff08;帧布局&#xff09;&#xff0c;她没有方便的定位方式&#xff0c;所有的控件都会默认摆放在布局的左上角。 修改activity_main.xml中的代码&#xff0c;如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <Frame…

mongodb 群集图_群集和重叠条形图

mongodb 群集图为什么和如何 (Why & How) 1.- Clustered Bar Charts1.- 集群条形图 AKA: grouped, side-by-side, multiset [bar charts, bar graphs, column charts]AKA &#xff1a;分组&#xff0c;并排&#xff0c;多组[条形图&#xff0c;条形图&#xff0c;柱形图] …

第一次写python

这是一个在BJDP上学习Coding Kata的时候用到的一个练习&#xff0c;原来打算用Java写的&#xff0c;但是一想正好是学习的好机会。 就用Python了。第一次&#xff0c;写的有些复杂。 这个题目是关于购买图书的打折信息的。 题目来源&#xff1a; http://codingdojo.org/cgi-bin…

Android 第八课 创建自定义控件

常用控件和布局的继承结构&#xff0c;如下图&#xff1a; &#xff08;待续。。。。&#xff09; 所有的控件都是直接或间接继承自View的&#xff0c;所用的所有布局都是直接或间接继承自ViewGroup的&#xff0c;View是Android中最基本的一种UI组件&#xff0c;它可以在屏幕上…

figma下载_搬到Figma对我意味着什么

figma下载A couple of years ago, amidst the boom of new design and prototyping software, I was pretty reluctant to fight on the Figma/Sketch cold war. I was working on a relatively small design team and, after years helping to design products, well sold on …

解决IE中img.onload失效的方法

解决IE中img.onload失效的方法 - CoffeeCats IT Blog - IT博客http://www.cnitblog.com/CoffeeCat/archive/2008/02/01/39533.htmlFirefox、Google Chrome不存在问题&#xff01;为什么onload没有被IE调用呢&#xff1f;因为IE会缓存图片&#xff0c;第2次加载的图片&#xff0…

Android 第九课 常用控件-------ListView

ListView允许用户通过手指上下滑动的方式将屏幕外的数据滚动到屏幕内&#xff0c;同时屏幕上原有数据将会滚动出屏幕。 1、ListView简单用法 如何将ListView将你要显示的大量内容关联起来呢&#xff1f;这是个很重要的问题。 1、首先我们必须先将数据提供好&#xff0c;因为你的…

Singleton patterns 单件(创建型模式)

1、模式分类 1.1 从目的来看&#xff1a; • – 创建型&#xff08;Creational&#xff09;模式&#xff1a;负责对象创建。 • – 结构型&#xff08;Structural&#xff09;模式&#xff1a;处理类与对象间的组合。 • – 行为型&#xff08;Behavioral&…

Android 第十一课 SQlite 数据库存储

Android 为了让我们能够更加方便的管理数据库&#xff0c;特意提供了一个SQLiteOpenHelper帮助类&#xff0c;通过借助这个类就可以非常简单的对数据库进行创建和升级。 SQLiteOpenHelper是一个抽象类&#xff0c;我们要创建一个自己的帮助类去继承它。SQLiteOpenHelper有两个抽…

浅析SQL Server 2005中的主动式通知机制

一、引言 在开发多人同时访问的Web应用程序&#xff08;其实不只这类程序&#xff09;时&#xff0c;开发人员往往会在缓存策略的设计上狠下功夫。这是因为&#xff0c;如果将这种环境下不常变更的数据临时存放在应用程序服务器或是用户机器上的话&#xff0c;可以避免频繁地往…

Android 第十二课 使用LitePal操作数据库(记得阅读最后面的注意事项哦)

一、LitePal简介 1、(新建项目LitePalTest)正式接触第一个开源库---LitePalLitePal是一款开源的Android 数据库框架&#xff0c;它采用了对象关系映射&#xff08;ORM&#xff09;的模式。2、配置LitePal&#xff0c;编辑app/build.gradle文件&#xff0c;在dependencies闭包中…

listview频繁刷新报错

在Android编程中使用Adapter时&#xff0c;偶尔会出现如下错误&#xff1a;The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI t…

Android 第十三课 SharedPreferences存储

SharedPreferences是使用键值对的方式来存储数据的。当保存一条数据时&#xff0c;需要给这条数据提供一个对应的键&#xff0c;这样在读取数据的时候就可以通过这个键把相应的值取出来。而且支SharedPreferences还支持多种不同的数据类型存储&#xff0c;例如&#xff1a;如果…

DSP的Gel作用

转自&#xff1a;http://blog.csdn.net/azhgul/article/details/6660960最近刚在研究Davinci系&#xff0c;特此MARK下&#xff0c;以资后续学习之用。 DSP的Gel作用 1 GEL文件基本作用 当CCSStudio启动时&#xff0c;GEL文件加载到PC机的内存中&#xff0c;如果定义了StartUp(…

解决关于登录校园网显示不在IP段的问题方案(要看注意事项哦!)

有时&#xff0c;登录校园网&#xff0c;账号和密码都显示正确&#xff0c;但是却显示出“账号只能在指定IP段登录”的问题。 那我们就提供了一个解决方案&#xff1a; 使用WinR,并在输入框&#xff0c;输入cmd命令&#xff1a;&#xff08;如下&#xff09;接着输入&#xff1…

jquery插件编写

jQuery为开发插件提拱了两个方法&#xff0c;分别是&#xff1a; jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。可以理解为添加静态方法。是全局的&#xff08;位于jQuery命名空间内部的函数&#xff09;…

gtk/Glade编程 编译命令不成功 解决方法

摘自&#xff1a;http://blog.chinaunix.net/uid-26746982-id-3433656.html 当我们编写gtk/glade程序&#xff0c;gcc编译时&#xff0c;用如下命令&#xff1a; #gcc -o server server.c pkg-config --cflags --libs gtk-2.0 报错&#xff1a;/tmp/ccoXadAd.o: In function …

Android 第十五课 如何使用LitePal从SQLite数据库中删除数据(十四课用来保留讲解如何向SQLite数据库中存入数据)

使用LitePal删除数据的方式主要有两种&#xff0c;第一种就是直接调用已存对象的delete()方法&#xff0c;所谓已存储对象就是调用过save()方法的对象&#xff0c;或者说是通过LitePal提供的查询API查出来的对象&#xff0c;都是可以直接使用delete方法来删除对象的。这是比较简…