【转】Android常用布局控件之RelativeLayout

来源:互联网 发布:java编写计算器 编辑:程序博客网 时间:2024/06/05 18:22

转自ITCEOjingying博客:http://blog.csdn.net/itceojingying/article/details/6302330        

        我们使用LinearLayout和TableLayout可以满足开发应用程序界面基本的要求。但是有时候实现界面的时候不够灵活,我们还可以使用另外一种控件RelativeLayout。RelativeLayout是一种相对布局的控件,这个容器内部的子元素们可以使用彼此之间的相对位置或者和容器间的相对位置来进行定位,类似于网页设计中的CSS。在指定控件的位置时,我们需要指定这个控件与其它控件之间的相对位置关系,比如说与另一个控件的左边对齐,右对齐,位于另一个控件的上方,下方等等。一个控件可以指定与多个其它控件的相对位置。这样,我们就可以在设计位置灵活多变的界面时就会更加的方便。 

Android RelativeLayout 属性

// 相对于给定ID控件

android:layout_above          将该控件的底部置于给定ID的控件之上;

android:layout_below          将该控件的底部置于给定ID的控件之下;

android:layout_toLeftOf      将该控件的右边缘与给定ID的控件左边缘对齐;

android:layout_toRightOf     将该控件的左边缘与给定ID的控件右边缘对齐;

 

android:layout_alignBaseline 将该控件的baseline与给定ID的baseline对齐;

android:layout_alignTop      将该控件的顶部边缘与给定ID的顶部边缘对齐;

android:layout_alignBottom   将该控件的底部边缘与给定ID的底部边缘对齐;

android:layout_alignLeft     将该控件的左边缘与给定ID的左边缘对齐;

android:layout_alignRight    将该控件的右边缘与给定ID的右边缘对齐;

// 相对于父组件

android:layout_alignParentTop         如果为true,将该控件的顶部与其父控件的顶部对齐;

android:layout_alignParentBottom      如果为true,将该控件的底部与其父控件的底部对齐;

android:layout_alignParentLeft        如果为true,将该控件的左部与其父控件的左部对齐;

android:layout_alignParentRight       如果为true,将该控件的右部与其父控件的右部对齐;

// 居中

android:layout_centerHorizontal      如果为true,将该控件的置于水平居中;

android:layout_centerVertical         如果为true,将该控件的置于垂直居中;

android:layout_centerInParent         如果为true,将该控件的置于父控件的中央;

// 指定移动像素,值为px

android:layout_marginTop           上偏移的值;

android:layout_marginBottom        下偏移的值;

android:layout_marginLeft        左偏移的值;

android:layout_marginRight        右偏移的值;

 示例:

<?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:padding="10px"    >           <TextView     android:id="@+id/textView"    android:text="TextView"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />    <!--没有对textView的位置做设置,默认为RelativeLayout容器的左上角  -->    <EditText    android:id="@+id/editText"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_below="@id/textView"    android:background="@android:drawable/editbox_background"    />    <!--editText控件位于textView控件的下面-->    <Button     android:id="@+id/buttonSure"    android:text="确定"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_below="@id/editText"    android:layout_alignParentRight="true"    android:layout_marginLeft="10px"    />    <!--buttonSure控件在editText控件的下面,并且于父容器的位置关系为右对齐。  android:layout_marginLeft="10px"                    设置buttonSure控件的左外边距为10像素,即此控件的左边与其他控件相距10像素的距离-->    <Button    android:id="@+id/buttonCancel"    android:text="取消"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_toLeftOf="@id/buttonSure"    android:layout_alignTop="@id/buttonSure"    />    <!--buttonCancel为相对于buttonSure的位置确定。buttonCancel控件的右边缘与buttonSure控件的左边缘对齐,                    顶部边缘与buttonSure控件的 顶部边缘对齐--></RelativeLayout>


运行效果:



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 国美买的格力空调发票丢了怎么办 国美实体店发票丢了怎么办 我在苏宁买的冰箱发票丢了怎么办 信用卡家庭电单位电话换了怎么办 常住户囗和实际住址没房子怎么办 退货淘宝极速退款 卖家拒收怎么办 淘宝退货极速退款后卖家拒收怎么办 蘑菇街付款成功怎么申请退款怎么办 淘宝上拍错地址联系卖家不管怎么办 收到货想退款卖家不同意怎么办 小米小店复合通过qq群怎么办 买了火车票在订单上查不到怎么办 用支付宝付款扣款没成功怎么办 在京东买东西只收到空盒子怎么办 魅族移动数据是灰色的怎么办? 魅族手机账号密码忘记了怎么办视频 淘宝购物退货把单号写错了怎么办 天猫想退货商家不给退货地址怎么办 天天爱消除四星宠物等级已满怎么办 店不干了店里面的财神怎么办 微信显示登陆注册语言打不开怎么办 三星手机解锁以后突然不量怎么办 耐克、阿迪品牌鉴定买到假货怎么办 小米4c盒子不支持有线连接怎么办 一个手机号绑着两个小米账号怎么办 小米5s手机听筒声音小怎么办 淘宝上买手机买到翻新怎么办 小米6用久了很卡怎么办 淘宝商家发错误的退货地址怎么办 买苹果手机时查到了翻新机该怎么办 荣耀9青春版玩王者卡怎么办 红米4x手机声音突然成破音怎么办 红米pro玩王者荣耀卡怎么办 红米手机恢复出厂设置出错了怎么办 红米恢复出厂设置出错了怎么办 应聘工作上当收押金了该怎么办 红米1s密码忘了怎么办 红米1s刷机后开不了机怎么办 红米2a耗电超快怎么办 红米2a手机耗电快怎么办 红米4x触屏失灵怎么办