如果有一个需求是这样的,在标题中的右上角有一个button

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/pink_light">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:layout_centerInParent="true"
/> <Button
android:layout_width="45dp"
android:layout_height="26dp"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:layout_centerVertical="true"
android:background="@color/green_light"
android:text="更多"
android:textSize="12sp"
/>
</RelativeLayout>
</LinearLayout>

其实上面的例子用到了相对布局的相对父控件,居中,靠右,距离右边多少db。当然相对父控件比较多,我们也给你自己定义一个参照物,比如,我们可以相对标题中的TextView作为参照物

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/pink_light">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题2"
android:layout_toRightOf="@+id/title"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
/>
</RelativeLayout>
</LinearLayout>

居中参照物

 <RelativeLayout
android:layout_width="match_parent"
android:background="@color/white"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
>
<Button
android:id="@+id/common_dialog_sureBtn"
android:layout_width="@dimen/common_button_width"
android:layout_height="@dimen/common_button_height"
android:background="@drawable/common_click_bule_btn_selector"
android:text="@string/sure"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/common_words_size"
android:layout_marginRight="30dp"
android:layout_toLeftOf="@+id/target_mid"/>
<View
android:id="@+id/target_mid"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/common_dialog_cancleBtn"
android:layout_width="@dimen/common_button_width"
android:layout_height="@dimen/common_button_height"
android:background="@drawable/common_click_gray_btn_selector"
android:text="@string/cencle"
android:gravity="center"
android:textColor="@color/white"
android:layout_toRightOf="@+id/target_mid"
android:layout_marginLeft="30dp"
android:textSize="@dimen/common_words_size"/>
</RelativeLayout>

此篇主要是 相对参照物

Android-RelativeLayout布局技巧(一)的更多相关文章

  1. Android RelativeLayout 布局android:layout_centerHorizontal="true"注意

    特别注意,如果要是 android:layout_alignTop="@id/bind_decode_item_layout" android:layout_centerHoriz ...

  2. android界面布局技巧(一)

    (1)//得到手机的宽高 Display display = getWindowManager().getDefaultDisplay(); int screenWidth = display.get ...

  3. Android学习笔记之布局技巧以及布局中的细节介绍....

    PS:休息两天,放一放手上的东西,做做总结... 学习内容: 1.Android中LinearLayout布局技巧... 2.layout中drawable属性的区别...   先简单的介绍一下dra ...

  4. Android开发——布局性能优化的一些技巧(一)

    0. 前言 上一篇我们分析了为什么LinearLayout会比RelativeLayout性能更高,意义在于分析了这两种布局的实现源码,算是对一个小结论的证明过程,但是对布局性能的优化效果,对这两种布 ...

  5. Android——android相对布局(RelativeLayout)及属性

    RelativeLayout布局 android:layout_marginTop="25dip" //顶部距离 android:gravity="left" ...

  6. Android相对布局(RelativeLayout)

    Android相对布局(RelativeLayout) 备注:这里的视图和元素是等同的概念. RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定).每个 ...

  7. 个人经验 - Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑

    Android的RelativeLayout布局的layout_height属性设置为wrap_content时的坑: 此坑出现的条件: 1.RelativeLayout布局的layout_heigh ...

  8. Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)

    首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变 ...

  9. Android 五大布局(LinearLayout、FrameLayout、AbsoulteLayout、RelativeLayout、TableLayout )

    前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处:          CSDN:http://www.csdn.net        ...

  10. android:layout_alignleft layout_toleftof区别,详解RelativeLayout布局属性

    转载请注明博客地址. 最近看博客看到有关于RelativeLayout布局的解释,有的解释很多是错误的.因此有必要对每一个常见的布局属性进行描述.以下解释全部都是逐行进行测试的. 首先把常用的布局分组 ...

随机推荐

  1. [git] warning: LF will be replaced by CRLF | fatal: CRLF would be replaced by LF[ git 处理和修改行结束符(CRLF和LF)]

    我自己的设置是: [core] autocrlf = false[core] safecrlf = true 取消自动转换CRLF(上图中选的是commit as is),但是有提交前混用检查 本人用 ...

  2. AC日记——pigs poj 1149

    POJ - 1149 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include <iostream> # ...

  3. 安装mezzanine时报:storing debug log for failure【已解决】

    同时还提示: bz2 module is not found(貌似) 解决方法: 1.重新安装python wget http://bzip.org/1.0.6/bzip2-1.0.6.tar.gz ...

  4. 如何在Android Studio中创建jniLib和asset文件夹 2

    1.创建asset文件夹 如图进行操作 2.创建jniLib文件夹 —打开app下面的gradle文件(不是project的gradle) —在gradle文件的Android标签里面添加 sourc ...

  5. HDU 3045 Picnic Cows

    $dp$,斜率优化. 设$dp[i]$表示$1$至$i$位置的最小费用,则$dp[i]=min(dp[j]+s[i]-s[j]-(i-j)*x[j+1])$,$dp[n]$为答案. 然后斜率优化就可以 ...

  6. 学习LSM(Linux security module)之二:编写并运行一个简单的demo

    各种折腾,经过了一个蛋疼的周末,终于在Ubuntu14.04上运行了一个基于LSM的简单demo程序. 一:程序编写 先简单的看一下这个demo: //demo_lsm.c#include <l ...

  7. [BZOJ 1804] Flood

    Link: BZOJ 1804 传送门 Solution: 不容易啊,第一道完全自己A掉的IOI题目..... 算法思想其实很简单: 模拟缩减的过程即可 将每条边转为2条有向边,每次找到最左边的边,沿 ...

  8. [LOJ6436]神仙的游戏

    感觉border的性质还是挺神奇的 一个border的性质是$S$有长度为$len$的border当且仅当对$\forall i\equiv j\left(\bmod(n-len)\right)$有$ ...

  9. [Contest20180328]同构

    我们先把两棵树的所有节点的儿子排序,然后dfs,记录访问的深度序列 然后可以发现题目中的两个操作都只会在深度序列中删掉一位,不会对其他位有影响(自己画画图就知道了) 对于一个深度序列$d_{1\cdo ...

  10. 【bitset】poj2443 Set Operation

    模板题.S[i][j]表示i是否存在于第j个集合里.妈蛋poj差点打成poi(波兰无关)是不是没救了. #include<cstdio> #include<bitset> us ...