RelativeLayout 如何实现平分布局空间

平分布局在LinearLayout中很简单,就是设置 layout_weight

但是我们有时候不得不在RelativeLayout 中实现平分布局空间,所以怎么做呢? 很简单

代码

<?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/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerVertical="true"/>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/strut"
        android:layout_alignParentTop="true"
        android:src="@drawable/img_artilce"/>

    <ImageView
        android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/strut"
        android:layout_alignParentBottom="true"
        android:src="@drawable/img_artilce"/>
</RelativeLayout>

这里写图片描述

是不是很简单?相信大家一看就能明白。
那水平平分呢?其实是一样的,只是换一下方向而已

代码

<?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/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true"/>

    <ImageView
        android:id="@+id/img1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignRight="@+id/strut"
        android:layout_alignParentLeft="true"
        android:src="@drawable/img_artilce"/>

    <ImageView
        android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/strut"
        android:layout_alignParentRight="true"
        android:src="@drawable/img_artilce"/>
</RelativeLayout>

这里写图片描述

好啦,就这样。有问题欢迎提出

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值