android 相对布局中不能存在循环依赖?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23904281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:35:07  来源:igfitidea点击:

Circular dependencies cannot exist in RelativeLayout, android?

androidandroid-layoutandroid-relativelayout

提问by WISHY

I have the following layout: I need to keep the button at the bottom of screen and it should be visible to the user. The rest of the layout should scroll.

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

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

I just need to do scroll my view and keep the button appeared on the screen at the bottom.

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

The problem is with

问题在于

     android:layout_above="@+id/bottomLinearLayout"

I get the error

我收到错误

     Circular dependencies cannot exist in RelativeLayout

What is wrong with my layout?

我的布局有什么问题?

回答by Soumil Deshpande

The problem is caused because there is a circular reference in the layout parameters.

问题是因为布局参数中存在循环引用。

For example, when view B is layout_below view A, view A can't reference view B anymore in it's below, alignRight etc. This can also exist between multiple views: A references B references C. In that scenario C can't reference A because of a circular dependency.

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

You Used :-

你用过:-

bottomLinearLayout is below scrollView1 And then you said that scrollView1 is above bottomLinearLayout

bottomLinearLayout 低于 scrollView1 然后你说 scrollView1 高于 bottomLinearLayout

It dosen't work like that. Use one

它不是那样工作的。使用一个

回答by Mgamerz

You cannot say the bottomLinearLayout is below scrollView1 then say scrollView1 is above bottomLinearLayout. Only do one, not both.

你不能说bottomLinearLayout 低于scrollView1 然后说scrollView1 高于bottomLinearLayout。只做一个,不要两个都做。

This is circular because it will try to position itself after it figures out where the other one is at... Which is waiting on the first one.

这是循环的,因为它会在确定另一个位置之后尝试定位自己......哪个正在等待第一个。

回答by adboco

Add android:layout_alignParentTop="true"in your scrollView and delete android:layout_below="@+id/scrollView1"in the bottomLinearLayout.

添加android:layout_alignParentTop="true"您的滚动视图并android:layout_below="@+id/scrollView1"在底部线性布局中删除。

回答by Manoj Rawat

If you give both view each other android:layout_belowid then this circular dependenciesproblem occur. for example

如果你给两个视图对方android:layout_belowid 那么这个循环依赖问题就会发生。例如

<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"/>