问题
Is it possible to do something like this where TV2 is relative to TV1, where TV1 and TV2 are within different branches of the layout?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/RV1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TV01"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="test text" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RV1" >
<TextView
android:id="@+id/TV02"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_toRightOf="@+id/TV01" <--------- this references TV01 within the top RelativeView !!!
android:text="other text" />
</RelativeLayout>
</RelativeLayout>
in this example the statement android:layout_toRightOf="@+id/TV01"
does not seem to work, since TV01
is within another RealtiveLayout branch than TV02
.
Is there a way to somehow make references like this?
Many thanks!
回答1:
No, you cannot use RelativeLayout positioning like that. However:
- You can combine
RV1
andRV2
or
- You can position
RV2
to the right ofRV1
回答2:
Is this a simplified example? or is this your actual layout. If the latter you should be able to achieve the same effect by changing rel2 to be like this:
<RelativeLayout
android:id="@+id/RV2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RV1"
android:layout_toRightOf="@+id/RV1" >
来源:https://stackoverflow.com/questions/11493693/relativelayout-referencing-ids-within-another-nested-view-is-this-possible