问题描述
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>