[Android布局]Relativelayout内部控件使用layout_toLeftOf或layout_toRightOf出错(会造成不能解析R文件)

问题描述

text属性为"18" 的TextView控件需要在ImageView左边,于是我就按从左到右的顺序写了控件布局,然后在TextView中加入layout_toLeftOf="@id/age_more"属性值,这导致R文件解析出错。

原因

在定义ImageView的id之前使用了该id,即"@id/age_more",会使编译程序不能找到该id。

解决办法

将id定义移动到使用之前,因为是相对布局,布局样式由属性决定,变量定义的顺序不会影响到实际的显示顺序。

错误写法

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/changecolor">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginBottom="20dp"
                    android:textSize="15dp"
                    android:text="年龄"
                    android:textColor="#000"/>
                    <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toLeftOf="@id/age_more"
                    android:layout_marginRight="10dp"
                    android:textSize="15dp"
                    android:text="18" />
                <ImageView
                    android:id="@+id/age_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/more"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="10dp"/>
            </RelativeLayout>

正确写法

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/changecolor">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="20dp"
                    android:layout_marginBottom="20dp"
                    android:textSize="15dp"
                    android:text="年龄"
                    android:textColor="#000"/>
                <ImageView
                    android:id="@+id/age_more"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/more"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="10dp"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_toLeftOf="@id/age_more"
                    android:layout_marginRight="10dp"
                    android:textSize="15dp"
                    android:text="18" />
            </RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值