(三)Android布局管理器-相对布局

1.基本属性

  1. android:layout_width:用来设置布局宽度。
  2. android:layout_height:用来设置布局高度。以上两个属性在布局管理器中一般设置为match_parent,即与父容器相同。
  3. android:paddingBotton:设置底内边距。 android:paddingLeft:设置左内边距。
  4. android:paddingRight:设置右内边距。
  5. android:paddingTop:设置顶内边距。以上四个属性可以设置为具体数值,比如16dp;也可以使用尺寸资源进行定义
  6. layout_alignParentBottom:父组件底部一条线,也就是并齐。
  7. layout_toRightOf:定义在谁的右边,紧紧相邻。
    这个布局与线性布局区别在orientation属性,相对布局是没有的

2.属性演示

本部分主要分两个模块
第一个使用layout_alignParentBottom 属性,字面意思是和父组件底部一条线,也就是并齐,以此类推,layout_alignParentRight意思是和父组件右部一条线
第二个介绍两个组件之间的相对关系,分别定义两个view组件,并利用id属性分别给两个组件定义了唯一标识,layout_toRightOf属性表示在谁的右边,后面填写了刚刚定义的view1的id,layout_below="@id/v2_1"可以看出定义在谁的下面。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#CC3352"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        ></View>
    <View
        android:id="@+id/v2_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#99FF00"></View>
    <View
        android:id="@+id/v2_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF0033"
        android:layout_toRightOf="@id/v2_1"
        android:layout_below="@id/v2_1"
        ></View>
</RelativeLayout>

在这里插入图片描述
利用已经学习的线性布局和相对布局综合一下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#00FFFF"
        android:layout_marginLeft="50dp">
    <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#CC3352"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        ></View>
    <View
        android:id="@+id/v2_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#99FF00"></View>
    <View
        android:id="@+id/v2_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FF0033"
        android:layout_toRightOf="@id/v2_1"
        android:layout_below="@id/v2_1"
        ></View></RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:background="#000000"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <View
            android:id="@+id/v3_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#9F79EE"></View>
        <View
            android:id="@+id/v3_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#9AFF9A"
            android:layout_marginLeft="10dp" ></View>
        <View
            android:id="@+id/v3_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#9F79EE"
            android:layout_marginLeft="10dp"></View>
    </LinearLayout>
</LinearLayout>

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值