补13

博客主要介绍安卓开发中的相对布局(RelativeLayout),包括常见相对布局基本属性,如根据父容器定位、根据兄弟组件定位及填充属性等。还讲述了创建安卓应用、编写主布局资源文件的过程,并展示了相对布局和按钮布局的运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相对布局RelativeLayout

输入姓名

常见相对布局基本属性:

  • gravity :设置该布局容器内各子组件的对齐方式

  • ignoreGravity:设置哪个组件不受gravity属性的影响

根据父容器定位

  • layout_alignParentLeft 左对齐
  • layout_alignParentRight 右对齐
  • layout_alignParentTop 顶部对齐
  • layout_centerVertical 垂直居中
  • layout_centerInparent 中间位置
  • layout_alignParentBottom 底部对齐

根据兄弟组件定位

  • android:layout_toLeftOf 参考组件的左边

  • android:layout_toRightOf 参考组件的右边

  • android:layout_above 参考组件的上方

  • android:layout_below 参考组件的下方

  • android:layout_alignTop 对齐参考组件的上边界

  • android:layout_alignBottom 对齐参考组件的下边界

  • android:layout_alignLeft 对齐参考组件的左边界

  • android:layout_alignRight 对齐参考组件的右边界

4、填充(padding)

  • android:padding 上下左右填充边距

  • paddingLeft 左边填充边距

  • paddingRight 右边填充边距

  • paddingTop 上方填充边距

  • paddingBottom 下方填充边距

创建安卓应用

在这里插入图片描述

编写主布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/edtName"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:hint="请输入姓名"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:text="姓名:"
        android:textSize="20dp"
        android:layout_alignBaseline="@+id/edtName"
        android:layout_toLeftOf="@+id/edtName"/>
    <Button
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="50dp"
        android:id="@+id/edtCancel"
        android:text="取消"
        />

    <Button
        android:id="@+id/edtOK"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"

        android:layout_marginTop="50dp"
        android:layout_marginRight="84dp"
        android:text="确定" />

</RelativeLayout>

运行效果

在这里插入图片描述

按钮布局

创建安卓应用

在这里插入图片描述

主布局资源文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.tp.relativelayoutdemo.MainActivity">

    <Button
        android:id="@+id/btn_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="中央" />

    <Button
        android:id="@+id/btn_upper_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_center"
        android:layout_above="@id/btn_center"
        android:text="左上"/>

    <Button
        android:id="@+id/btn_upper_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn_center"
        android:layout_above="@id/btn_center"
        android:text="右上"/>

    <Button
        android:id="@+id/btn_lower_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/btn_center"
        android:layout_below="@id/btn_center"
        android:text="左下"/>

    <Button
        android:id="@+id/btn_lower_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/btn_center"
        android:layout_below="@id/btn_center"
        android:text="右下"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_lower_left"
        android:layout_alignLeft="@id/btn_lower_left"
        android:layout_marginTop="15dp"
        android:text="确定"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_lower_right"
        android:layout_alignRight="@id/btn_lower_right"
        android:layout_marginTop="15dp"
        android:text="取消"
        />

    <Button
        android:id="@+id/btn_upper_left_corner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="左上角"/>

    <Button
        android:id="@+id/btn_upper_right_corner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="右上角"/>

    <Button
        android:id="@+id/btn_lower_left_corner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="左下角"/>
    <Button
        android:id="@+id/btn_lower_right_corner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="右下角"/>


</RelativeLayout>

运行效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值