相对布局(RelativeLayout)

RelativeLayout中往往需要定义每一个控件的资源ID

layout_toLeftOf      layout_toRightOf    layout_below表示相对位置

layout_alignXXX表示对齐方式

layout_marginXXX表示间距

android:layout_centerInParent="true":将控件放置于父界面的中心位置

即子控件的位置可以取决于兄弟控件或者父控件的位置,与LinearLayout相比,减少嵌套使用,界面代码简洁.


实例代码 : (下列代码效果是由以中间的控件为参照物来定位的)

<?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">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Java"
        android:layout_centerInParent="true"
        android:textSize="30sp"
        android:textColor="#F000"
        android:id="@+id/textView_center" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左上"
        android:textSize="30sp"
        android:id="@+id/textView_left_top"
        android:layout_above="@+id/textView_center"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="60dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="右上"
        android:textSize="30sp"
        android:id="@+id/textView_right_top"
        android:layout_alignTop="@+id/textView_left_top"
        android:layout_alignParentEnd="true" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="左下"
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
相对布局RelativeLayout)是 Android 中常用的一种布局方式,可以根据控件之间的相对位置来确定它们的布局关系。以下是使用相对布局的步骤: 1. 在 XML 布局文件中,使用 `<RelativeLayout>` 标签将布局内容包裹起来。 2. 在 `<RelativeLayout>` 标签内部,使用 `<View>` 或其他布局控件标签来定义需要布局的控件。 3. 对于每个需要布局的控件,可以使用一些属性指定它们与其他控件的相对位置。常用属性包括: - `android:layout_alignParentTop`、`android:layout_alignParentBottom`、`android:layout_alignParentLeft`、 `android:layout_alignParentRight`:将控件与父容器的顶部、底部、左侧或右侧对齐。 - `android:layout_alignTop`、`android:layout_alignBottom`、`android:layout_alignLeft`、`android:layout_alignRight`: 将控件与指定控件的顶部、底部、左侧或右侧对齐。 - `android:layout_below`、`android:layout_above`、`android:layout_toLeftOf`、`android:layout_toRightOf`: 将控件放置在指定控件的下方、上方、左侧或右侧。 - `android:layout_centerInParent`:将控件居中于父容器。 - `android:layout_centerHorizontal`、`android:layout_centerVertical`:将控件水平或垂直居中于父容器。 这些属性可以与其他布局属性(如 `android:layout_width`、`android:layout_height`)结合使用,以确定控件的大小和位置。 4. 使用这些属性为每个控件设置相应的相对位置关系,调整它们的顺序和布局方式,以满足设计需求。 下面是一个示例: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" android:layout_below="@id/button1" android:layout_alignParentRight="true"/> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" android:layout_toLeftOf="@id/button2" android:layout_below="@id/button1" android:layout_marginRight="10dp"/> </RelativeLayout> ``` 在这个示例中,有三个按钮控件,它们的位置相互关联。第一个按钮位于父容器的顶部和左侧,第二个按钮位于第一个按钮的下方和父容器的右侧,第三个按钮位于第一个按钮和第二个按钮之间,并稍微向右偏移了一些距离。 这样,通过相对布局,我们可以根据控件之间的相对位置来灵活地排列和定位界面元素。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值