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

把一条线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. Team Foundation Server 2015使用教程--默认团队权限说明

  2. Controller 里面就只应该存放这些不能复用的代码(转)

    MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...

  3. 一个sql的优化

    原文:一个sql的优化 目的:为了查询某天某个服务器上的登录id的个数   刚开始编写的sql: select count(a.mac) logusers from Log_MacLogin_All ...

  4. 利用纯CSS3实现超立体的3D图片侧翻倾斜效果

    原文:利用纯CSS3实现超立体的3D图片侧翻倾斜效果 上午的时候我在jQuery论坛上看到网友分享的一款CSS3 3D图片侧翻倾斜特效,觉得效果非常棒,其实话说回来,这玩意儿的实现真的非常简单,主要是 ...

  5. Ubuntu 15.10 x64 安装 Android SDK(转)

    操作系统:Ubuntu 15.10 x64 目标:安装 Android SDK 本文最后更新时间:2015-11-3 安装32位库文件 2013年9月的iPhone 5s是第一款64位手机,而Andr ...

  6. POJ 2411 Mondriaan&#39;s Dream (dp + 减少国家)

    链接:http://poj.org/problem?id=2411 题意:题目描写叙述:用1*2 的矩形通过组合拼成大矩形.求拼成指定的大矩形有几种拼法. 參考博客:http://blog.csdn. ...

  7. SAP RFC 函数来创建 Java呼叫 学习总结 一步一步的插图

    前言 该公司很快就接到了一个项目,SAP有接口.让我们做老大SAP.首先SAP联系.但发展从来没有打过.本周集中在这一个研究. 各种碰壁,SAP该系统让我怎么说? 算了.说多了都是泪,以下附上本周学习 ...

  8. Android——保存并读取文件

    Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,仅仅能被应用本身訪问,在该模式下,写入的内容会覆盖原文件的内容,假设想把新写入的内容追加到原文件里.能够使用Contex ...

  9. Android发展简报

    Android这个词的本义是指“机器人”.同时它是Google至2007年11月5根据公布Linux台的开源手机操作系统的名称,该平台由操作系统.中间件.用户界面和应用软件组成.号称是首个为移动终端打 ...

  10. 汉高澳大利亚sinox接口捆绑经典winxp,全面支持unicode跨语言处理

    用qtconfig(或者qtconfig-qt4)设置字体后,汉澳sinox视窗界面以跟winxp媲美的界面出现,爽心悦目. 并且视窗使用非常稳定.非常少出现死机无响应现象,堪称完美. 引入unico ...