【Android基础】利用布局来拼图

本文通过实例解析如何使用相对布局在Android中拼接碎片形成完整图片,包括布局代码、完成效果及可能遇到的问题分析。

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

这次我们来讲一个拼图的例子,主要讲解下相对布局。

使用布局将下面的碎片排列成一张完整的图片:

这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述
这里写图片描述 这里写图片描述 这里写图片描述
这里写图片描述

完成效果图:

这里写图片描述

布局文件代码如下:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.yzx.layout.MainActivity" >

    <ImageView
        android:id="@+id/center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/center" />

    <ImageView
        android:id="@+id/buttom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/center"
        android:layout_centerHorizontal="true"
        android:src="@drawable/buttom" />

    <ImageView
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/center"
        android:layout_centerHorizontal="true"
        android:src="@drawable/top" />

    <ImageView
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/center"
        android:layout_centerVertical="true"
        android:src="@drawable/left" />

    <ImageView
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/center"
        android:layout_centerVertical="true"
        android:src="@drawable/right" />

    <ImageView
        android:id="@+id/lefttop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/left"
        android:layout_alignLeft="@id/left"
        android:layout_alignRight="@id/left"
        android:src="@drawable/lefttop" />

    <ImageView
        android:id="@+id/righttop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/right"
        android:layout_alignLeft="@id/right"
        android:layout_alignRight="@id/right"
        android:src="@drawable/righttop" />

    <ImageView
        android:id="@+id/leftbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/left"
        android:layout_alignLeft="@id/left"
        android:layout_alignRight="@id/left"
        android:src="@drawable/leftbutton" />

    <ImageView
        android:id="@+id/rightbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/right"
        android:layout_alignLeft="@id/right"
        android:layout_alignRight="@id/right"
        android:src="@drawable/rightbutton" />

</RelativeLayout>

不知道是图片裁剪的不平整还是我布局不好,最后显示的结果长短不一,希望各位给我指出来错误。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值