3.相对布局管理器

什么是相对布局管理器?

首先确定参考点,依据参考点进行布局。
注:作为参考点的组件可以是兄弟组件或者是父容器。

两个属性(在布局管理器上):

android:gravity---------------设置布局管理器中各个子组件的摆放方式的
android:ignoreGravity------哪个属性不受前面的影响

相对布局管理器提供了一个内部类:RealativeLayout.LayoutParams(在组件上设置属性)

组件相对于参考组件位置(boolean类型)
android:layout_above
android:layout_below
android:layout_toLeftOf
android:layout_toRightOf
组件于布局管理器哪边对齐
android:layout_alignParentBottom
…Left
…Right
…Top
组件与哪个组件的上下左右边界对齐
android:layout_alignBottom
…Left
…Right
…Top
组件位于布局管理器的哪个位置
android:layout_centerHorizontal
android:layout_centerInParent
android:layout_centerVertical

案例实现飞机大战界面

<?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"
    android:background="@mipmap/plane"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/t1"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发现有新版本,现在安装吗?"
      >
    </TextView>

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/t1"
        android:layout_below="@+id/t1"
        android:text="以后再说"
        >
    </Button>

    <Button
        android:id="@+id/b2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/b1"
        android:layout_below="@+id/t1"
        android:text="现在更新"
        >
    </Button>
</RelativeLayout>

结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43328054/article/details/106357218
今日推荐