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

把一条线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. SVD奇异值分解的几何物理意义资料汇总

    学习SVD奇异值分解的网上资料汇总: 1. 关于svd的一篇概念文,这篇文章也是后续几篇文章的鼻祖~ http://www.ams.org/samplings/feature-column/fcarc ...

  2. Spark1.0.0 学习路径

          2014-05-30 Spark1.0.0 Relaease 经过11次RC后最终公布.尽管还有不少bug,还是非常令人振奋. 作为一个骨灰级的老IT,经过非常成一段时间的消沉,再次被点燃 ...

  3. mariadb 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    [root@localhost /]# systemctl stop mariadb.service[root@localhost /]# mysqld_safe --user=mysql --ski ...

  4. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  5. ABP之动态WebAPI

    ABP之动态WebAPI ABP的动态WebApi实现了直接对服务层的调用(其实病没有跨过ApiController,只是将ApiController公共化,对于这一点的处理类似于MVC,对服务端的 ...

  6. IOS ARC和非ARC文件混用

    ARC在SDK4.0的时候增加的,因为要和曾经的项目融合,就会有arc和非arc文件的混合. 当然,也就这两种情况: 1.自己的旧项目没有使用ARC,可是引入的第三方库却是使用了ARC的. 2.自己的 ...

  7. Lucene40PostingWriter

    控制freq和prox这两个文件的输出,比较简单. 默认skip interval这是16,max skip level这是10. 由源或使用可见VInt编码(而不是速度的传奇PForDelta). ...

  8. HTTPS背后的加密算法(转)

    当你在浏览器的地址栏上输入https开头的网址后,浏览器和服务器之间会在接下来的几百毫秒内进行大量的通信.InfoQ的这篇文章对此有非常详细的描述.这些复杂的步骤的第一步,就是浏览器与服务器之间协商一 ...

  9. ASP.NET 5 Web 项目

    在Mac OS X Yosemite 10.10.3 中搭建第一个 ASP.NET 5 Web 项目 终于有时间在 Mac 上安装一下 ASP.NET 5,网上有许多教程,但是多数的时间比较早了,版本 ...

  10. CSharp设计模式读书笔记(6):建造者模式(学习难度:★★★★☆,使用频率:★★☆☆☆)

    建造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 模式角色与模式: 建造者模式可以将一个产品的内部表象与产品的生产过程分割开来, ...