2020-09-27 RelativeLayout 相对布局笔记

一 、相对布局

RelativeLayout 相对布局

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

1 页面右下布局

android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"!

2 在**@+id/view_1**方块的右面

android:layout_toRightOf="@id/view_1"

3 在**@+id/view_1**方块的下面

android:layout_below="@+id/view_1"

线性和相对代码综合:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <View
        android:id="@+id/view_1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#000000"/>
    <View
        android:id="@+id/view_2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00FF80"
        android:layout_below="@+id/view_1"/>
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@id/view_2"
        android:orientation="horizontal"
        android:background="#AEB404"
        android:padding="15dp" >
            <View
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#B40404"/>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:padding="15dp">
            <View
                android:id="@+id/view_3"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#00FF80"
                android:layout_marginRight="15dp"
                />
            <View
                android:id="@+id/view_4"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:background="#A9A9F5"
                android:layout_toRightOf="@id/view_3"
                />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

在这里插入图片描述
注意:android:id=“xxxxx”

使用:驼峰式命名法

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,相对布局的标签是`RelativeLayout`。 `RelativeLayout`是一种常用的布局方式,它允许我们将控件相对于其他控件或父布局进行定位。我们可以通过设置控件之间的相对位置关系,来实现不同的布局效果。 例如,以下代码展示了如何使用`RelativeLayout`来实现一个简单的布局: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, World!" android:layout_centerInParent="true" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me!" android:layout_below="@id/textView1" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 在代码中,`RelativeLayout`标签用于定义一个相对布局,`TextView`和`Button`控件分别设置了不同的相对位置关系。例如,`TextView`控件使用了`layout_centerInParent`属性,表示该控件应该居中于父布局中。而`Button`控件则使用了`layout_below`和`layout_centerHorizontal`属性,表示该控件应该位于`TextView`控件的下方,并且水平居中于父布局中。 需要注意的是,`RelativeLayout`布局方式比较灵活,可以根据实际需求设置不同的相对位置关系。同时,由于需要计算控件之间的相对位置关系,因此相对布局可能会比其他布局方式更加耗费资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值