Android笔记(七) Android中的布局——相对布局

RelativeLayout又称为相对布局,也是一种常用的布局形式。和LinearLayout的排列规则不同,RelativeLayout显得更加随意一下,它通常通过相对定位 的方式让控件出现在布局的任何位置。也正是因为如此,RelativeLayout中的属性非常多,不过这些属性都是有规律可循的。

我们通过代码来看:

relativelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button 1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button 2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_centerInParent="true"
        android:text="Button 3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Button 4"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="Button 5"/>
</RelativeLayout>

  运行结果为:

    

由此可见,这些属性和他们的名字一样,分别代表的含义为:

android:layout_alignParentLeft——和父布局的左对齐

android:layout_alignParentTop——和父布局上部对齐

android:layout_alignParentRight——和父布局的右对齐

android:layout_alignParentBottom——和父布局的下部对齐

android:layout_centerInParent——在父布局中居中显示

上面代码我们的控件是以父布局为标准的,在RelativeLayout中,同样也可以以控件为标准进行定位。

代码示例:

relativelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        android:layout_centerInParent="true"
        android:text="Button 3"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:layout_above="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="Button 1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        android:layout_above="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="Button 2"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        android:layout_below="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="Button 4"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button5"
        android:layout_below="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="Button 5"/>

</RelativeLayout>

  运行结果为:

  

这些属性也有规律可循:

android:layout_above——表示该控件在标准控件上方

android:layout_below——表示该控件在标准控件下方

android:layout_toLeftOf——表示该控件在标准控件左面

android:layout_ toRightOf——表示该控件在标准控件右面

但需要注意的是,这些属性需要使用“@id/xxxx”来引入标准控件的id,并且当一个控件以另外一个控件为标准时,另一个控件一定要定义在这个控件前面,否则会出现找不到id的情况。

RelativeLayout中还有另外一组相对于控件进行定位的属性

android:layout_alignRight——表示该控件的右边缘和标准控件的右边缘对齐

android:layout_ alignLeft——表示该控件的左边缘和标准控件的左边缘对齐

android:layout_alignTop——表示该控件的上边缘和标准控件的上边缘对齐 android:layout_alignBottom——表示该控件的下边缘和标准控件的下边缘对齐

时间: 2024-07-29 02:29:00

Android笔记(七) Android中的布局——相对布局的相关文章

Android笔记:java 中的数组

在与嵌入式设备通讯的过程中使用的socket通讯 获取的字节流,通常转换为字节数组,需要根据协议将字节数组拆分.对于有规律的重复拆分可以使用,由于java中不能像c中直接进行内存操作例如使用struct 内存拷贝可以直接实现,这里考虑的是借用数组拷贝的方式来实现. java数组复制参考 引自百度空间 同时参考 数组拷贝之java.lang.System.arraycopy和java.util.Arrays.copyOf方法 java数组复制的4种方式,java数组拷贝问题 JAVA数组的复制是引

iOS流布局UICollectionView系列七——三维中的球型布局

摘要: 类似标签云的球状布局,也类似与魔方的3D布局 iOS流布局UICollectionView系列七--三维中的球型布局 一.引言 通过6篇的博客,从平面上最简单的规则摆放的布局,到不规则的瀑布流布局,再到平面中的圆环布局,我们突破了线性布局的局限,在后面,我们将布局扩展到了空间,在Z轴上进行了平移,我们实现了一个类似UIPickerView的布局模型,其实我们还可以再进一步,类比于平面布局,picKerView只是线性排列布局在空间上的旋转与平移,这次,我们更加充分了利用一下空间的尺寸,来

【Android笔记】Fragment中显示高德地图

本文来自:fair-jm.iteye.com 转截请注明出处 官网的教程是在Activity下 在Fragment下在高德论坛找到一些方法 试了下可以显示 但是切换后总会有些问题 比如切换后就是新的了 切换后地图就不显示了 我这种方式可以在切换后保持地图状态 但是得限定屏幕为水平或者竖直 如果翻转的话也会报错 布局文件: 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns

Android基础:代码中实现界面动态布局

<RelativeLayout 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" > <TextView

Android笔记(七十二) Style和Theme

我们尝尝需要使用setText.setColor.setTextSize等属性来设置控件的样式,但是每个控件都需要设置这些属性,工作量无疑是巨大的,并且后期维护起来也不方便. Style Android中的样式(style)包含一组格式,为一个组件设置使用某个样式时,该样式所包含的全部格式都将会应用在这个组件上. Android的样式资源文件放在/res/values目录下,其跟元素是<resources>,该元素内包含多个<style>子元素,每个子元素都是一个样式. <s

Android笔记(七十三) Android权限问题整理 非常全面

Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permission.ACCESS_COARSE_LOCATION通过WiFi或移动基站的方式获取用户错略的经纬度信息,定位精度大概误差在30~150

自学Android笔记——在activity中使用intent

一.Intent介绍: Intent的中文意思是"意图,意向",在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用.Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互.因此,可以将Intent理解为不同组件之间通信的"媒介&quo

【Android笔记】Android创建启动画面

每个Android应用启动之后都会出现一个Splash启动界面,显示产品的LOGO.公司的LOGO或者开发者信息.如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间. 1.制作Splash界面 突出产品LOGO,产品名称,产品主要特色: 注明产品的版本信息: 注明公司信息或者开发者信息: 背景图片,亦可以用背景颜色代替: 2.处理后台资源 大多数的Splash界面都是会等待一定时间,然后切换到下一个界面: 其实,在这段时间里,可以对系统状况进行检测,比如

【Android笔记】Android的三种网络通信方式

Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口).Org.apache接口和Android.net.*(Android网络接口).下面分别介绍这些接口的功能和作用. 1.标准Java接口java.net.*提供与联网有关的类,包括流.数据包套接字(socket).Internet协议.常见Http处理等.比如:创建URL,以及URLConnection/HttpURLConnection对象.设置链接参数.链接到服务器.向服务器写数据.从服务器读取数

【Android笔记】Android与服务器数据库通信的方法

1.Android平台下与服务器数据库通信的方法 在Android平台下,连接电脑服务器的MySQL.PostgreSQL.Oracle.Sybase.Microsoft SQLServer等数据库管理系统DBMS(database management system),主要有以下两种方法: 方法1.直接连接 在Android工程中引入JDBC驱动,直接连接.(本文主要介绍此种方法) 方法2.间接连接 在服务器上用PHP+DBMS做服务器端,PHP将DBMS中的数据用json或者xml进行封装.