zoukankan      html  css  js  c++  java
  • 相对布局

          相对布局由RelativeLayout代表,相对布局容器内子组件的位置总是相对兄弟组件、父容器来决定的,因此这种布局方式被称为相对布局。

          如果A组件的位置是由B组件的位置来决定的,Android要求先定义B组件,再定义A组件。

          RelativeLayout可支持如表2.8所示的两个XML属性。

          表2.8 RelativeLayout的XML属性及相关方法说明

    XML属性 相关方法 说明
    android:gravity setGraviy(int) 设置该布局容器内各子组件的对齐方式
    android:ignoreGravty setIgnoreGravity(int) 设置哪个组件不受gravity属性的影响

        为了控制该布局管理器中各子组件的布局分布,RelativeLayout提供了一个内部类:RelativeLayout.LayoutParams,该类提供了大量的XML属性来控制RelativeLayout布局管理器中子组件的布局分布。

         RelativeLayout.LayoutParams里只能设为true、false的XML属性如表2.9所示。

         表2.9  RelativeLayout.LayoutParams里只能设为boolean值的属性

    android:layout_centerHorizontal 控制该子组件是否位于布局容器的水平居中
    android:layout_centerVertical 控制该子组件是否位于布局容器的垂直居中
    android:layout_centerInParent 控制该子组件是否位于布局容器的中央位置
    android:layout_alignParentBottom 控制该子组件是否与布局容器低端对齐
    android:layout_alignParentLeft 控制该子组件是否与布局容器左边对齐
    android:layout_alignParentRIght 控制该子组件是否与布局容器右边对齐
    android:layout_alignParentTop 控制该子组件是否与布局容器顶端对齐

         RelativeLayout.Layoutarams里属性值为其他UI组件ID的XML属性如表2.10所示。

           表2.10 RelativeLayout.LayoutParams里只能设为其他UI组件ID的属性

    android:layout_toRightOf 控制该子组件位于给出ID组件的右侧
    android:layout_toLeftOf 控制该子组件位于给出ID组件的左侧
    android:layout_above 控制该子组件位于给出ID组件的上方
    android:layout_below 控制该子组件位于给出ID组件的下方
    android:layout_alignTop 控制该子组件位于给出ID组件的上边界对齐
    android:layout_alignBotton 控制该子组件位于给出ID组件的下边界对齐
    android:layout_alignLeft 控制该子组件位于给出ID组件的左边界对齐
    android:layout_alignRight 控制该子组件位于给出ID组件的右边界对齐

     实例:棉花布局效果

    相对布局容器中的子组件总是相对其他组件来决定分布位置的,可以考虑先把一个组件放在相对布局容器的中间,然后以该组件为中心,将其他组件分布在该组价的四周,这样就可以形成“梅花布局”效果。

        下面是“梅花”布局效果的界面布局文件。

       

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000"
       >
      <!-- 定义该组件位于父容器中间 -->
        <TextView
            android:id="@+id/view01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/leaf"
            android:layout_centerInParent="true" />
         <!-- 定义该组件位于view01组件的上方 -->
        <TextView
            android:id="@+id/view02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/leaf"
            android:layout_above="@id/view01"
            android:layout_alignLeft="@id/view01"
            />
          <!-- 定义该组件位于view01组件的下方 -->
        <TextView
            android:id="@+id/view03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/leaf"
            android:layout_below="@id/view01"
            android:layout_alignLeft="@id/view01"
            />
         <!-- 定义该组件位于view01组件的左边 -->
        <TextView
            android:id="@+id/view04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/leaf"
            android:layout_toLeftOf="@id/view01"
            android:layout_alignTop="@id/view01"
            />
          <!-- 定义该组件位于view01组件的右边 -->
        <TextView
            android:id="@+id/view05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/leaf"
            android:layout_toRightOf="@id/view01"
            android:layout_alignTop="@id/view01"
            />
    
    </RelativeLayout>

    运行Activity可以看到图2.13的效果

     

  • 相关阅读:
    5-python基础—获取某个目录下的文件列表(适用于任何系统)
    Automated, Self-Service Provisioning of VMs Using HyperForm (Part 1) (使用HyperForm自动配置虚拟机(第1部分)
    CloudStack Support in Apache libcloud(Apache libcloud中对CloudStack支持)
    Deploying MicroProfile-Based Java Apps to Bluemix(将基于MicroProfile的Java应用程序部署到Bluemix)
    Adding Persistent Storage to Red Hat CDK Kit 3.0 (在Red Hat CDK Kit 3.0添加永久性存储)
    Carve Your Laptop Into VMs Using Vagrant(使用Vagran把您笔记本电脑刻录成虚拟机)
    使用Python生成一张用于登陆验证的字符图片
    Jupyter notebook的安装方法
    Ubuntu16.04使用Anaconda5搭建TensorFlow使用环境 图文详细教程
    不同时区的换算
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3340728.html
Copyright © 2011-2022 走看看