二Relative Layout

Relative Layout布局:相对位置布局,的几个重要属性

  第一类:属性值为true或false

  •   android:layout_centerHrizontal 水平居中
  •   android:layout_centerVertical 垂直居中
  •   android:layout_centerInparent 相对于父元素完全居中
  •   android:layout_alignParentBottom 贴紧父元素的下边缘
  •   android:layout_alignParentLeft 贴紧父元素的左边缘
  •   android:layout_alignParentRight 贴紧父元素的右边缘
  •   android:layout_alignParentTop 贴紧父元素的上边缘
  •   android:layout_alignWithParentIfMissing 若找不到兄弟元素以父元素做参照物

第二类:属性值必须为id的引用名“@id/id-name”

  •   android:layout_below 在某元素的下方
  •   android:layout_above 在某元素的上方
  •   android:layout_toLeftOf 在某元素的左边
  •   android:layout_toRightOf 在某元素的右边
  •   android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  •   android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  •   android:layout_alignBottom本元素的下边缘和某元素的的下边缘对齐
  •   android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  第三类:属性值为具体的像素值,如30dip,40px

  •   android:layout_marginBottom 离某元素底边缘的距离
  •   android:layout_marginLeft 离某元素左边缘的距离
  •   android:layout_marginRight 离某元素右边缘的距离
  •   android:layout_marginTop 离某元素上边缘的距离

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

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

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

<?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" >    <TextView        android:id="@+id/label"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Type here:" />    <EditText        android:id="@+id/entry"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/label"        android:background="@android:drawable/editbox_background" />    <Button        android:id="@+id/ok"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@id/entry"        android:layout_marginLeft="10dip"        android:text="OK" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@id/ok"        android:layout_toLeftOf="@id/ok"        android:text="Cancel" /></RelativeLayout>

最后的效果图为:

还需知道:

1.android:paddingLeft与android:layout_marginLeft的区别:

padding margin都是边距的含义,关键问题得明白是什么相对什么的边距.
padding是控件的内容相对控件的边缘的边距.
layout_margin是控件边缘相对父空间的边距.

2.android: gravityandroid:layout_gravity区别

  android:gravity 属性是对该view 内容的限定.比如一个button 上面的text.你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个.
android:layout_gravity是用来设置该view相对与起父view 的位置.比如一个button 在linearlayout里,你想把该button放在靠左靠右等位置就可以通过该属性设置.

这样就解释了,有什么我们弄个最外布局,然后里面包了几个布局,如果要使这几个布局都靠底,就可以在最外布局的属性里设置androi:gravity="botton"因为gravity是对里面的内容起作用.


更多相关文章

  1. Android之创建实时文件夹
  2. Android(安卓)PreferenceActivity的介绍
  3. view-ListView学习
  4. [置顶] Android资源文件分析
  5. android 设置全屏的两种方法
  6. android light
  7. android 打开系统设置界面
  8. Android(安卓)自定义CheckBox 样式
  9. Android设置Button各种状态下背景改变

随机推荐

  1. Android的设计模式-享元模式
  2. TextView
  3. Android绘图之PathMeasure(16)
  4. android scrollview组件禁止滑动的方法
  5. android中控件属性大全
  6. Android(安卓)SDK Android(安卓)NDK 官方
  7. android狼人杀源码,桌面源码,猎豹快切源码
  8. android 让多余的文字不显示
  9. 50个Android开发人员必备UI效果源码[转载
  10. android之控件布局的几个小案例