• Android学习笔记08-相对布局RelativeLayout


    RelativeLayout是实际布局中最常用的布局方式之一。RelativeLayout可以设置某一个视图相对于其他视图的位置,这些位置可以包括上下左右等,因而相较于其他的布局方式而言具有很大的灵活性。

      RelativeLayout的常用属性有以下一些:

      第一类:属性值为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 本元素的右边缘和某元素的的右边缘对齐

      第三类:属性值为具体的像素值
      android:layout_marginBottom 离某元素底边缘的距离
      android:layout_marginLeft 离某元素左边缘的距离
      android:layout_marginRight 离某元素右边缘的距离
      android:layout_marginTop 离某元素上边缘的距离

      下面是一个使用相对布局的实例。activity_main.xml源码如下:

     1 Android_RelativeLayout实例
     2  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3      xmlns:tools="http://schemas.android.com/tools"
     4      android:layout_width="match_parent"
     5      android:layout_height="match_parent" >
     6      
     7      <!-- 居中的按钮:参照物 -->
     8      <Button
     9          android:id="@+id/mButton_center"
    10          android:text="@string/center"
    11          android:layout_centerHorizontal="true"
    12          android:layout_centerVertical="true"
    13          android:layout_width="90dp"
    14          android:layout_height="wrap_content"    >        
    15      </Button>
    16      
    17      <!---->
    18      <Button
    19          android:id="@+id/mButton_above"
    20          android:text="@string/above"
    21          android:layout_above="@id/mButton_center"
    22          android:layout_centerHorizontal="true"
    23          android:layout_width="90dp"
    24          android:layout_height="wrap_content"    >        
    25      </Button>
    26      
    27      <!---->
    28      <Button
    29          android:id="@+id/mButton_below"
    30          android:text="@string/below"
    31          android:layout_below="@id/mButton_center"
    32          android:layout_centerHorizontal="true"
    33          android:layout_width="90dp"
    34          android:layout_height="wrap_content"    >        
    35      </Button>
    36      
    37      <!---->
    38      <Button
    39          android:id="@+id/mButton_left"
    40          android:text="@string/left"
    41          android:layout_toLeftOf="@id/mButton_center"
    42          android:layout_centerVertical ="true"
    43          android:layout_width="120dp"
    44          android:layout_height="wrap_content"    >        
    45      </Button>
    46      
    47      <!---->
    48      <Button
    49          android:id="@+id/mButton_right"
    50          android:text="@string/right"
    51          android:layout_toRightOf="@id/mButton_center"
    52          android:layout_centerVertical ="true"
    53          android:layout_width="120dp"
    54          android:layout_height="wrap_content"    >        
    55      </Button>    
    56      
    57      <!-- 左上 -->
    58      <Button
    59          android:id="@+id/mButton_aboveAndleft"
    60          android:text="@string/aboveAndleft"
    61          android:layout_above="@id/mButton_center"
    62          android:layout_toLeftOf="@id/mButton_above"
    63          android:layout_width="120dp"
    64          android:layout_height="wrap_content"    >        
    65      </Button>
    66      
    67      <!-- 右上 -->
    68      <Button
    69          android:id="@+id/mButton_aboveAndright"
    70          android:text="@string/aboveAndright"
    71          android:layout_above="@id/mButton_center"
    72          android:layout_toRightOf="@id/mButton_above"
    73          android:layout_width="120dp"
    74          android:layout_height="wrap_content"    >        
    75      </Button>
    76      
    77      <!-- 左下 -->
    78      <Button
    79          android:id="@+id/mButton_belowAndleft"
    80          android:text="@string/belowAndleft"
    81          android:layout_below="@id/mButton_center"
    82          android:layout_toLeftOf="@id/mButton_below"
    83          android:layout_width="120dp"
    84          android:layout_height="wrap_content"    >        
    85      </Button>            
    86      
    87      <!-- 右下 -->
    88      <Button
    89          android:id="@+id/mButton_belowAndright"
    90          android:text="@string/belowAndright"
    91          android:layout_below="@id/mButton_center"
    92          android:layout_toRightOf="@id/mButton_below"
    93          android:layout_width="120dp"
    94          android:layout_height="wrap_content"    >        
    95      </Button>
    96  </RelativeLayout>

    效果图如图1所示:

    图1:Android_RelativeLayout实例

  • 相关阅读:
    Object-c学习之路七(oc字符串操作)
    Object-c学习之路六(oc字符串文件读写)
    Object-c学习之路五(@protocol协议)
    jQ效果(滑动)
    jQ效果(淡入淡出)
    jQ效果(显示隐藏)
    jQ笔记2
    jq笔记
    DOM节点操作
    两个css样式
  • 原文地址:https://www.cnblogs.com/britalient/p/3173113.html
Copyright © 2020-2023  润新知