I have two widgets within a RelativeLayout that must reference each other. Technically it is not a circular reference since the widget A is vertically aligned with widget B and widget B is horizontally aligned with widget A. Here is my code (condensed):
<Button android:id="@+id/btnLanguageFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/imgArrow"
android:text="English" />
<ImageView android:id="@+id/imgArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true"
android:layout_alignBottom="@id/btnLanguageFrom"
android:layout_centerHorizontal="true"
android:src="@drawable/arrow_right" />
However when I build I get this error:
Error: No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/imgArrow').
Interestingly, the Graphical Layout view in Eclipse displays it correctly and doesn't complain about the circular reference.
I don't see the problem with two widgets referencing each other along different dimensions (horizontal and vertical) since it cannot cause an infinite loop. Is there any way around this problem? This is the only way I know to get the layout I need.
Thanks in advance, Barry P.S. Is there any way to declare an id in advance, like in C/C++?
The first time you reference an ID, use the @+ prefix, which tells the resource builder to add the ID, rather than trying to find it. So try:
android:layout_toLeftOf="@+id/imgArrow"
A circular reference is the thing you want to avoid. But to solve your problem just switch the definition of your Button
and ImageView
so that the ImageView
you're refering in the Button
is defined first. For some reasons it is not possible to do it the other way round.
Additionally I would always use the same refernence type. So in your case: "@+id/imgArrow"
来源:https://stackoverflow.com/questions/5681324/how-do-i-enforce-a-circular-reference-in-a-relativelayout