RelativeLayout - referencing ids within another nested view - is this possible?

时光怂恿深爱的人放手 提交于 2019-12-25 04:18:00

问题


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 and RV2

or

  • You can position RV2 to the right of RV1



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!