Android布局之二——Relative Layout

因工作原因,有十天左右没有学习了,感觉总差点什么似的,心里总是记着还有Android,哎,但愿不要半途而废^_^ !!!!!!!!!!


Relative Layout

Relative Layout布局:相对位置布局,类似于Word中的位置对齐,总共有九个方位,如下图所示
1 

下面,我们来看看布局文件的代码与详解:

 
 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

RelativeLayout 声明此Activity使用相对布局,
android:layout_width="fill_parent":宽度填充父控件的宽度
android:layout_height="fill_parent":高度填充父控件的高度

 

TextView    //一个标签
android:id="@+id/label"    //组件ID
android:layout_width="fill_parent"    //宽,填充父控件
android:layout_height="wrap_content" //高,刚刚包含自己的内容的高度,相当于自适应
android:text="Type here:"    //标签显示的内容

EditText    //一个文本框
android:id="@+id/entry"    //组件ID
android:layout_width="fill_parent"    //宽
android:layout_height="wrap_content"    //高
android:background="@android:drawable/editbox_background"    //背景色
android:layout_below="@id/label"    //below英文单词译为在…下,这里引用前面的标签组件,意为摆在这个标签的下面

Button    //一个按钮
android:id="@+id/ok"    //组件ID
android:layout_width="wrap_content"    //宽度自适应
android:layout_height="wrap_content"    //高度自适应
android:layout_below="@id/entry"    //在前面组件文本框的下面
android:layout_alignParentRight="true" //相对父控件右对齐,这里的父控件应该是屏幕,即屏幕的右边
android:layout_marginLeft="10dip"//组件的外边距10个像素,margin,padding可以看看CSS的讲解.
android:text="OK"//按钮上显示的内容

Button
android:layout_width="wrap_content"//宽自适应
android:layout_height="wrap_content"//高自适应
android:layout_toLeftOf="@id/ok" //使当前控件置于id为ok的控件的左边。
android:layout_alignTop="@id/ok" //使当前控件与id为ok控件的上端排齐。
android:text="Cancel"//按钮上显示的内容

可以看到相对位置的对齐方式有以下图中所示的这么多:
image 
可以分别对应Word中的九个方位,以及还可以设置类似CSS中的组件与组件之间的外边距,如margin即是设置这一项,注意layout_margin这一项设置像素以后,则是设置的上下左右四个方位的值,如果只想设置某一边的话,则使用下面的marginLeft……之一,
设置组件内文字与组件边距可用属性padding,如下图:
image

最后效果图为
image

好了,相对布局就介绍到到这里,休息休息,一休哥, 哥的哥的哥的哥的……

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值