在RelativeLayout,android中不能存在循环依赖?

时间:2014-05-28 06:30:17

标签: android android-layout android-relativelayout

我有以下布局: 我需要将按钮保持在屏幕的底部,并且应该对用户可见。 布局的其余部分应滚动。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.vfc.sargroup.SalesOrderActivity$PlaceholderFragment" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/bottomLinearLayout" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Select Distributor Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Payment Collection"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Product Category"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <TableLayout
            android:id="@+id/salesTableLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:stretchColumns="*" >
        </TableLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total QTY"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Total Price"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remark"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" >
        </EditText>
    </LinearLayout>
</ScrollView>

<LinearLayout
    android:id="@+id/bottomLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/scrollView1"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />
</LinearLayout>
  </RelativeLayout>

我只需要滚动我的视图并保持按钮出现在屏幕底部。

问题在于

     android:layout_above="@+id/bottomLinearLayout"

我收到错误

     Circular dependencies cannot exist in RelativeLayout

我的布局有什么问题?

5 个答案:

答案 0 :(得分:66)

问题是由于布局参​​数中存在循环引用而引起的。

例如,当视图B是layout_below视图A时,视图A不能再参考其中的视图B,alignRight等。这也可以存在于多个视图之间:引用B引用C.在那种情况下,由于循环依赖,C不能引用A.

你使用过: -

bottomLinearLayout位于scrollView1下方 然后你说scrollView1在bottomLinearLayout

之上

这样做是不行的。 使用一个

答案 1 :(得分:4)

你不能说bottomLinearLayout在scrollView1之下然后说scrollView1在bottomLinearLayout之上。只做一个,而不是两个。

这是循环的,因为它会在找出另一个人所在的位置后试图定位自己......正在等待第一个。

答案 2 :(得分:0)

在您的scrollView中添加android:layout_alignParentTop="true"并删除bottomLinearLayout中的android:layout_below="@+id/scrollView1"

答案 3 :(得分:0)

如果您同时给出两个视图的 android:layout_below ID,则会出现此循环依赖项问题。 例如

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Example Series"
        android:layout_below="@+id/rv_qpl"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_qpl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv"/>

答案 4 :(得分:-1)

将滚动视图封闭在相对布局中。

结构将是:

<RelativeLayout
......
....
.....>

<RelativeLayout
.....
.....
.....>

<ScrollView
....
...
...>

put your staff here

</ScrollView>
</RelativeLayout>


<RelativeLayout
....
....
below the above relative layout
...../>

<Button
....
....
..../>

</RelativeLayout>

</RelativeLayout>
相关问题