在使用RelativeLayout布局的时候出现的错误,主要原因还是对于布局的细节原理不够理解,思维固定:
错误代码:
<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!--①--> <ImageView android:id="@+id/play_activity_pre" android:layout_width="40dp" android:layout_height="40dp" android:background="@mipmap/prev" /> <!--②--> <ImageView android:id="@+id/play_activity_play" android:layout_width="40dp" android:layout_height="40dp" android:layout_toLeftOf="@id/paly_activity_next" android:background="@mipmap/play" /> <!--③--> <ImageView android:id="@+id/paly_activity_next" android:layout_width="40dp" android:layout_height="40dp" android:background="@mipmap/next" android:layout_alignParentRight="true" android:layout_marginRight="15dp" />
报错:Error:No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/paly_activity_next').
原因: 在相对布局中使用 Layout_toLeftOf等属性的时候,一定要先将参照物先写在别的空件之前的位置,否者会出现找不到参照物名称的错误。
若打算①②③顺序排列的图片,若先以③为参照物,需先将③控件写出。即在写的时候顺序为③②①