AndroidStudio相对布局RelativeLayout

发布于:2022-12-02 ⋅ 阅读:(170) ⋅ 点赞:(0)

相用到的是对布局<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="com.example.tset1.MainActivity">

    <Button
        android:id="@+id/zhong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中"
        android:layout_centerInParent="true" />
    <Button
        android:id="@+id/bei"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="北"
        android:layout_above="@id/zhong"
        android:layout_alignLeft="@id/zhong"/>
    <Button
        android:id="@+id/dong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="东"
        android:layout_toRightOf="@id/zhong"
        android:layout_alignTop="@id/zhong"/>
    <Button
        android:id="@+id/xi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="西"
        android:layout_toLeftOf="@id/zhong"
        android:layout_alignTop="@id/zhong"/>
    <Button
        android:id="@+id/nan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="南"
        android:layout_below="@id/zhong"
        android:layout_alignLeft="@id/zhong"/>
</RelativeLayout>
用相对布局,将“中”作为被参考控件,并android:id="@+id/zhong"命名,通过ndroid:layout_alignLeft、android:layout_alignTop、android:layout_below等属性使东南西北有序排列。
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到