RelativeLayout布局id找不到的问题

本文介绍在使用Android的RelativeLayout布局时,如何正确放置视图组件以避免因布局加载顺序导致的引用错误。特别是当使用相对于其他视图定位的属性时需要注意的事项。

最近犯过几次这样的错。

在RelativeLayout布局里,有两个控件,如图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4CBAEB">


    <TextView
        android:id="@+id/tv_xiala"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="下拉刷新"
        android:textColor="@color/white"
        android:textSize="30sp" />

    <ImageView
        android:id="@+id/img_jiantou"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginTop="15dp"
        android:layout_toLeftOf="@id/tv_xiala"
        android:background="@drawable/head_me" />
</RelativeLayout>

第一个控件是 TextView,id是tv_xiala
第二个控件是ImageView,id是img_jiantou

因为在第二个控件里用到了第一个控件的id
android:layout_toLeftOf=”@id/tv_xiala”

所以第二个控件必须写在第一个控件的下边,因为安卓读取布局的时候是从上往下读的,如果放在上面就会出现找不到id的情况啦。
所以如果用到
android:layout_toLeftOf
android:layout_toRightOf=””
android:layout_above=””
android:layout_below=””
这些属性的时候要小心啦。虽然不是什么大问题,但是记录一下,巩固一下知识吧。
布局id找不到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值