转自: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. 洛谷——P1109 学生分组

    P1109 学生分组 题目描述 有N组学生,给出初始时每组中的学生个数,再给出每组学生人数的上界R和下界L(L<=R),每次你可以在某组中选出一个学生把他安排到另外一组中,问最少要多少次才可以使 ...

  2. 【UOJ #198】【CTSC 2016】时空旅行

    http://uoj.ac/problem/198 (先补一下以前的题解) 这道题5分暴力好写好调,链上部分分可以用可持久化线段树,每次旅行\(x\)值相同的可以用标记永久化线段树.我还听到某些神犇说 ...

  3. 某5道CF水题

    1.PolandBall and Hypothesis 题面在这里! 大意就是让你找一个m使得n*m+1是一个合数. 首先对于1和2可以特判,是1输出3,是2输出4. 然后对于其他所有的n,我们都可以 ...

  4. FZU 2105 Digits Count(按位维护线段树)

    [题目链接] http://acm.fzu.edu.cn/problem.php?pid=2105 [题目大意] 给出一个序列,数字均小于16,为正数,每次区间操作可以使得 1. [l,r]区间and ...

  5. 【推导】【贪心】Codeforces Round #431 (Div. 1) A. From Y to Y

    题意:让你构造一个只包含小写字母的可重集,每次可以取两个元素,将它们合并,合并的代价是这两个元素各自的从‘a’到‘z’出现的次数之积的和. 给你K,你构造的可重集必须满足将所有元素合而为一以后,所消耗 ...

  6. 【判断解是否可行-二分】POJ1064-Cable master

    http://poj.org/problem?id=1064 [题目大意] 给出几条绳子的长度,问如果要切出k条长度相等的绳子,这k条绳子最常多长? [思路] 二分.把下界设为0,上界设为所有绳子长度 ...

  7. 找出最小元素的下标 Exercise07_10

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:找出最小元素的下标 * */ public class Exercise0 ...

  8. 5月学习总结(Ant-Design,mustache,require.js,grunt)

    一.Ant-Design学习 因为Ant-Design是基于React实现的,之前自己也学过一段时间的React,对React还是相对比较熟悉的,在学习Ant-Design也还不算吃力. 最开始是从源 ...

  9. Word中插入英文格式的算法流程

    如图上部分所示,需要序号自动编号,那么插入一个一行一列的的表格,然后点击编号,使得项目自动编号,编号一个就在后面输入一些内容,按enter后第二行开始自动编号,如果要缩进,不能按Tab,Tab自动创建 ...

  10. 安装maven,eclipse及eclipse配置maven

    现在的eclipse,maven安装非常简单.下载解压就可以用. 官网上下载eclipse, https://www.eclipse.org/downloads/eclipse-packages/ 选 ...