在设计过程中,我们经常会遇到这样的需求:

把一条线3控制,左对齐左控制,右侧控制右对齐,中间控制,以填补剩余空间。

或者一列内放3个控件,上面的与顶部对齐,以下的沉在最底部,中间控件是弹性的。充满剩余空间。

情况一:水平布局

图示:

这是第一种情形。因为涉及到ImageView。想保持图片原比例不便使用LinearLayout的weight属性。

解决的方法:

1.外层套一个RelativeLayout

2.三个控件分别装进3个LinearLayout中。假如id分别为leftlayout,midlayout,rightlayout

leftlayout属性:android:layout_width="wrap_content"

rightlayout属性:android:layout_width="wrap_content"

midlayout属性: android:layout_width="match_parent"

android:layout_toLeftOf="@+id/rightlayout"

                                   android:layout_toRightOf="@+id/leftlayout"

这样就能够达到两端控件分别左右对齐。中部控件填充剩余空间的效果。

上图效果的布局图示:

上图效果的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="34dp"
android:background="#FFFFFF"
android:orientation="horizontal" > <LinearLayout
android:id="@+id/choosetags_listview_item_leftlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"> <ImageView
android:id="@+id/taglistview_item_ico"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:contentDescription="@string/app_name"
android:src="@drawable/tag_ico_movie" /> </LinearLayout> <LinearLayout
android:id="@+id/choosetags_listview_item_midlayout"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/choosetags_listview_item_rightlayout"
android:layout_toRightOf="@+id/choosetags_listview_item_leftlayout" > <com.coolletter.util.MarqueeTextView
android:id="@+id/taglistview_item_name"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:marqueeRepeatLimit="marquee_forever"
android:paddingEnd="5dp"
android:paddingStart="5dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="#000000"
android:textSize="15dp" /> </LinearLayout> <LinearLayout
android:id="@+id/choosetags_listview_item_rightlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" > <TextView
android:id="@+id/taglistview_item_newnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="253"
android:textColor="#000000" > </TextView> </LinearLayout> </RelativeLayout>

情形二:垂直布局

图示:

垂直布局方案:

1.外层放一个RealtiveLayout

2.内部三个控件分别装进3个LinearLayout中,id设为topayout。midlayout,bottomlayout

toplayout属性:android:layout_width="wrap_content"

bottomlayout属性:android:layout_width="wrap_content"

midlayout属性: android:layout_width="match_parent"

android:layout_below="@+id/toplayout"

                                   android:layout_above="@+id/bottomlayout"

布局:

代码:

<?xml version="1.0" encoding="utf-8"?

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DCDCDC"
android:orientation="vertical" > <LinearLayout
android:id="@+id/letter_newtext_toplayout"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:background="#FFFAF0"
android:orientation="horizontal" > <TextView
android:id="@+id/letter_newtext_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Cancel"
android:textColor="#000000"
android:textSize="16dp" /> <TextView
android:id="@+id/letter_newtext_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Submit"
android:textColor="#000000"
android:textSize="16dp" /> </LinearLayout> <LinearLayout
android:id="@+id/letter_newtext_mainlayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/letter_newtext_deliver"
android:layout_below="@+id/letter_newtext_toplayout"
android:orientation="vertical"
> <EditText
android:id="@+id/letter_newtext_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/corners_bg"
android:gravity="top"
android:inputType="textMultiLine"
android:textColor="#000000" /> </LinearLayout> <View
android:id="@+id/letter_newtext_deliver"
android:layout_above="@+id/letter_newtext__bottomlayout"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:background="#00BFFF" /> <LinearLayout
android:id="@+id/letter_newtext__bottomlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="bottom"
android:orientation="horizontal" > <ImageView
android:id="@+id/letter_newtext_ico_tag"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:background="@drawable/letter_new_ico_maintag" /> <TextView
android:id="@+id/letter_newtext_tag_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColor="#000000"
android:textSize="15dp" /> </LinearLayout> </RelativeLayout>

当这样的情况中间的控件是一个ScrollView时,也使用这样的办法。

就能实现ScrollView充满上下两个控件中间的区域。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

【Android开发日记】妙用 RelativeLayout 实现3 段布局的更多相关文章

  1. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  2. 【Android开发日记】之入门篇(七)——Android数据存储(上)

    在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也 ...

  3. 【Android开发日记】之入门篇(八)——Android数据存储(下)

    废话不多说了,紧接着来讲数据库的操作吧.Come On! 提到数据存储问题,数据库是不得不提的.数据库是用来存储关系型数据的不二利器.Android为开发者提供了强大的数据库支持,可以用来轻松地构造基 ...

  4. 【Android开发日记】之入门篇(九)——Android四大组件之ContentProvider

    数据源组件ContentProvider与其他组件不同,数据源组件并不包括特定的功能逻辑.它只是负责为应用提供数据访问的接口.Android内置的许多数据都是使用ContentProvider形式,供 ...

  5. 【Android开发日记】之入门篇(五)——Android四大组件之Service

    这几天忙着驾校考试,连电脑都碰不到了,今天总算告一段落了~~Service作为Android的服务组件,默默地在后台为整个程序服务,辅助应用与系统中的其他组件或系统服务进行沟通.它跟Activity的 ...

  6. 【Android开发日记】之入门篇(六)——Android四大组件之Broadcast Receiver

    广播接受者是作为系统的监听者存在着的,它可以监听系统或系统中其他应用发生的事件来做出响应.如设备开机时,应用要检查数据的变化状况,此时就可以通过广播来把消息通知给用户.又如网络状态改变时,电量变化时都 ...

  7. 【Android开发日记】之入门篇(十二)——Android组件间的数据传输

    组件我们有了,那么我们缺少一个组件之间传递信息的渠道.利用Intent做载体,这是一个王道的做法.还有呢,可以利用文件系统来做数据共享.也可以使用Application设置全局数据,利用组件来进行控制 ...

  8. 【Android开发日记】之入门篇(四)——Android四大组件之Activity

    在Android中,无论是开发者还是用户,接触最多的就算是Activity.它是Android中最复杂.最核心的组件.Activity组件是负责与用户进行交互的组件,它的设计理念在很多方面都和Web页 ...

  9. 【Android开发日记】之入门篇(十三)——Android的控件解析

    Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...

随机推荐

  1. (插播)unity的 异常捕捉和 ios Android 崩溃信息的捕捉。

    近期 做些准备性得工作和有意思的事情.所以近期做了一个适合ios和android 错误信息捕捉的unity插件. 两个功能,app崩溃也就是闪退 是开发人员 非常头疼的一件事,还有就是一些莫名得错误 ...

  2. SQLserver创建与主外键的看法

    一个.背景 最初研究的相关内容数据库.仅仅是正式.从来没有练过,只能慢慢漂流,现在做的客房时,,非常多的知识需要使用视图,慢的实践. 视图:我理解的就是一张表.它把我们所须要的某个表或某几个表中的部分 ...

  3. git 如何让单个文件回退到指定的版本

    1.进入到文件所在文件目录,或者能找到文件的路径查看文件的修改记录 1 $ git log MainActivity.java 结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  4. HTTP的REST服务简介

      REST简介   一说到REST,我想大家的第一反应就是“啊,就是那种前后台通信方式.”但是在要求详细讲述它所提出的各个约束,以及如何开始搭建REST服务时,却很少有人能够清晰地说出它到底是什么, ...

  5. Nyoj 吝啬的国度(图论&&双DFS)

    描述在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市,必须经过的前一个城市是几号城市(假设你 ...

  6. 采用jquery的imgAreaSelect样品图像裁剪示范插件实现

    将用户上传的图片进行裁剪再保存是如今web2.0应用中经常处理的工作,如今借助jquery的imgareaselect插件再配合PHP的GD库就能够轻松的实现这个在曾经来说很棘手的功能. 我们来看看它 ...

  7. HDU 1598 find the most comfortable road (最小生成树) &gt;&gt;

    Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...

  8. NPOI实现Excel导入导出

    NPOI实现Excel的导入导出,踩坑若干. Cyan是博主[Soar360]自2014年以来开始编写整理的工具组件,用于解决现实工作中常用且与业务逻辑无关的问题. 什么是NPOI? NPOI 是 P ...

  9. 饼干怪兽和APT攻击

    APT攻击就像一个孩子,你通过各种方式窃取他们的大脑要拿出饼干,为了防止恶意攻击,过失作为母亲未能发现和防止饼干盗窃贼如.于她仅仅监视厨房椅子.衣柜门或烤箱门的开启.建立起有效防御目标攻击与APT攻击 ...

  10. 详细的图文介绍如何利用XAMPP本地建站的环境配置教程

    原文:详细的图文介绍如何利用XAMPP本地建站的环境配置教程 WordPress 是一个简便快捷,用途广,人气旺的一个开源的博客建站程序.很有很多等您去发现. 简便快捷:在性能上易于操作.易于浏览: ...