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的效果

     

  • 相关阅读:
    《软件性能测试与LoadRunner实战教程》新书上市
    《你必须掌握的Entity Framework 6.x与Core 2.0》正式出版感想
    《你必须掌握的Entity Framework 6.x与Core 2.0》书籍出版
    别跟我谈EF抵抗并发,敢问你到底会不会用EntityFramework
    EntityFramework Core 运行dotnet ef命令迁移背后本质是什么?(EF Core迁移原理)
    已被.NET基金会认可的弹性和瞬态故障处理库Polly介绍
    WebAPi之SelfHost自创建证书启动Https疑难解惑及无法正确返回结果
    Web APi之认证(Authentication)两种实现方式【二】(十三)
    读懂操作系统之虚拟内存TLB与缓存(cache)关系篇(四)
    读懂操作系统之缓存原理(cache)(三)
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3340728.html
Copyright © 2011-2022 走看看