转自:http://blog.csdn.net/onepiece2/article/details/26396287

RelativeLayout

是相对布局在页面上相对于页面坐标进行布局设置。比如可以通过确定对象A确定对象B的位置,B可以在A的上下左右,对象B距离A的位置。

RelativeLayout的灵活性很高,但在实际操作过程中我很难确定定位对象的位置,最后用图形界面手托才完成页面的布局。

页面截图如下

实现代码如下

 <?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" > <TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tipUserName"
android:layout_alignParentTop="true"
android:text="@string/user_name"
android:textSize="20sp" /> <EditText
android:id="@+id/tipUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/userName"
android:inputType="text"
android:text="@string/tip_user_name" /> <TextView
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_below="@id/userName"
android:layout_toLeftOf="@+id/tipUserName"
android:text="@string/user_password"
android:textSize="20sp" /> <EditText
android:id="@+id/tipPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tipUserName"
android:layout_toRightOf="@id/passWord"
android:inputType="textPassword"
android:text="@string/tip_user_password" /> <Button
android:id="@+id/logInBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/passWord"
android:text="@string/login_Btn" /> <CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tipPassword"
android:layout_toRightOf="@id/logInBtn"
android:text="@string/rember_pass" /> </RelativeLayout>

用户名和密码字体过小,可用android:textSize="XXsp"调整字体大小。

我本来想通过用户名框的位置定下用户名输入框的位置和密码框的位置,再通过密码框的位置定位到密码输入框的和登陆按钮的位置,最后通过登陆按钮来确定单选框的位置。最后结果就是由于用户名框和密码框太小,导致部分挤在了一起,特别难看。

LinearLayout

线性布局也是一种比较灵活的布局方式, 通过直接的线性布局对页面直接实现布局。

在使用LinearLayout 的时候,思路大致是将页面母板分成若干部分,然后母板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"
tools:context="${packageName}.${activityClass}"
tools:ignore="Orientation" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/userName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name" /> <EditText
android:id="@+id/tipUserName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"
android:text="@string/tip_user_name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/passWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_password" /> <EditText
android:id="@+id/tipPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
android:text="@string/tip_user_password" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <Button
android:id="@+id/logInBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login_Btn" /> <CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rember_pass" />
</LinearLayout> </LinearLayout>
在使用LinearLayout 的时候要注意将各个对象包裹进 LinearLayout 。已经有了母板LinearLayout 就不用再重行创建LinearLayout 面板了。对于LinearLayout 的实现思路还是挺清晰的,所以部署起来还是不算很难。

LinearLayout和RelativeLayout

共有属性
java代码中通过btn1关联次控件
android:id="@+id/btn1"

控件宽度
android:layout_width="80px" //"80dip"或"80dp"
android:layout_width =“wrap_content”
android:layout_width =“match_parent”

控件高度
android:layout_height="80px" //"80dip"或"80dp"
android:layout_height =“wrap_content”
android:layout_height =“match_parent”

控件排布
android:orientation="horizontal”
android:orientation="vertical“

控件间距
android:layout_marginLeft="5dip" //距离左边
android:layout_marginRight="5dip" //距离右边
android:layout_marginTop="5dip" //距离上面
android:layout_marginRight="5dip" //距离下面

android:paddingLeft="5dip"

控件显示位置
android:gravity="center" //left,right, top, bottom
android:gravity="center_horizontal"

android:layout_gravity是本元素对父元素的重力方向。
android:layout_gravity属性则设置控件本身相对于父控件的显示位置
android:gravity是本元素所有子元素的重力方向。

android:layout_gravity="center_vertical"
android:layout_gravity="left"
android:layout_gravity="left|bottom"

TextView中文本字体
android:text="@String/text1" //在string.xml中定义text1的值
android:textSize="20sp"
android:textColor=”#ff123456”
android:textStyle="bold" //普通(normal), 斜体(italic),粗斜体(bold_italic)

TextView中,控制其以...结束

android:ellipsize="end"

只有一行

android:singleLine="true"

定义控件是否可见
android:visibility=”visible” //可见
android:visibility=”invisible”  //不可见,但是在布局中占用的位置还在
android:visibility=”gone”   //不可见,完全从布局中消失

定义背景图片
android:background="@drawable/img_bg" //img_bg为drawable下的一张图片

seekbar控件背景图片及最大值
android:progressDrawable="@drawable/seekbar_img" 
android:thumb="@drawable/thumb"     
android:max = "60"

android:layout_alignWithParentIfMissing="true"

仅在RelativeLayout中有效
在父亲布局的相对位置
android:layout_alignParentLeft="true" //在布局左边
android:layout_alignParentRight="true" //在布局右边
android:layout_alignParentTop="true" //在布局上面
android:layout_alignParentBottom="true " //在布局的下面

在某个控件的相对位置
android:layout_toRightOf="@id/button1" //在控件button1的右边,不仅仅是紧靠着
android:layout_toLeftOf="@id/button1" //在控件button2的左边,不仅仅是紧靠着
android:layout_below="@id/button1 " //在控件button1下面,不仅仅是正下方
android:layout_above=“@id/button1” //在控件button1下面,不仅仅是正下方

定义和某控件对奇
android:layout_alignTop=”@id/button1” //和控件button1上对齐
android:layout_alignBottom=”@id/button1” //和控件button1下对齐
android:layout_alignLeft=”@id/button1” //和控件button1左对齐
android:layout_alignRight=”@id/button1” //和控件button2右对齐

android:layout_centerHorizontal="true"   //水平居中
android:layout_centerVertical="true"
android:layout_centerInParent="true"

仅在LinearLayout中有效
设置控件在一排或一列中所占比例值
android:layout_weight="1"

 

RelativeLayout与LinearLayout的比较的更多相关文章

  1. Android -------- RelativeLayout 和 LinearLayout 的性能分析

    布局的绘制角度 RelativeLayout不如LinearLayout快的根本原因是: RelativeLayout需要对其子View进行两次measure过程, 而LinearLayout则只需一 ...

  2. 如何优化你的布局层级结构之RelativeLayout和LinearLayout及FrameLayout性能分析

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/51159419 如何优化你的布局层级结构之RelativeLayout和LinearLa ...

  3. Android中RelativeLayout和LinearLayout性能分析

    先看一些现象吧:用eclipse或者Android studio,新建一个Activity自动生成的布局文件都是RelativeLayout,或许你会认为这是IDE的默认设置问题,其实不然,这是由 a ...

  4. RelativeLayout与LinearLayout的区别

    1.LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的 containers,超过边界时,某些控件将缺失或消失.因此一个垂 ...

  5. Relativelayout和LinearLayout对比分析

    分析之前先了解下View的绘制流程 首先view在windows中的布局样式如下图: view绘制在windows,windows与DecoverView的交互在VIewRoot中进行. view绘制 ...

  6. 设置布局默认为LinearLayout,却成了RelativeLayout

    GoogleXML布局文件前推荐布局LinearLayout新建布局XML文件根元素LinearLayout, 随着android发展工程师更推荐使用RelativeLayout布局式所新建XML布局 ...

  7. Android开发——LinearLayout和RelativeLayout的性能对比

    0. 前言 我们都知道新建一个Android项目自动生成的Xml布局文件的根节点默认是RelativeLayout,这不是IDE默认设置,而是由android-sdk\tools\templates\ ...

  8. 使用RelativeLayout控制WebView以及Bottom按钮的位置

    使用RelativeLayout控制WebView以及Bottom按钮的位置 (地址) 在Design View中加入控件RelativeLayout, WebView, LinearLayout(H ...

  9. 【腾讯Bugly干货分享】Android动态布局入门及NinePatchChunk解密

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57c7ff5d53bbcffd68c64411 作者:黄进——QQ音乐团队 摆脱 ...

随机推荐

  1. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  2. 从Linux下载文件到Windows没有换行问题

    这是一个小问题,一般用txt打开文件才会遇到,word打开也是正常(估计其他编程软件打开也正常). 顺便提一下pscp从Linux上下载文件到Windows. C:\Users\xuefei>p ...

  3. js实现table导出为Excel文件

    1.首先创建好表格. 2.然后在js中写三个方法 1)判断浏览器 2)定义文档类型 template : 定义文档的类型,相当于html页面中顶部的<!DOCTYPE> 声明.(个人理解, ...

  4. [BZOJ1860][ZJOI2006]Mahjong(DP)

    1860: [Zjoi2006]Mahjong麻将 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 412  Solved: 248[Submit][Sta ...

  5. 【推导】计蒜客17119 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 F. Trig Function

    题意:给你n,m,让你求cos(nx)的展开式的(cos(x))^m项的系数. 更一般的式子是这样的:. 队友的代码: #include<cstdio> #include<algor ...

  6. iOS .a静态库的制作及使用

    一.制作.a静态库 1.创建静态库工程,工程命名为BaseSDK,生成的.a文件名变成libBaseSDK.

  7. opensuse个人初始化

    Auth: jin Date: 20140414 sourcedir=/mnt/shelldir=/home/shell/userdir= /home/jin#mount /dev/sdb1 /mnt ...

  8. [Winform]线程间操作无效,从不是创建控件的线程访问它的几个解决方案,async和await?

    目录 概述 取消跨线程检查 使用委托异步调用 sync和await 总结 概述 最近在qq群里有一朋友,问起在winform中怎么通过开启线程的方式去处理耗时的操作,比如,查看某个目录下所有的文件,或 ...

  9. (转)c#中判断是不是数字和字母

    一.判断字母string str = Console.ReadLine();if (char.isLetter(str)){ }else if (char.IsDigit(str)){ }if(ch& ...

  10. SDK里报错[NSConcreteMutableData wbsdk_base64EncodedString]

    百度一大堆都说在这个里加个-ObjC,然后加了还是有问题 最近谷歌了下才要加入这个才正常了,国内的开发者只是一知半解的………… 如果错误还没有解决, 下面这个可以帮到你: