Android Relative Layout circular dependencies erro

2020-02-01 04:21发布

问题:

Hi im trying to create a custom layout but i'm struggling to get it to work properly i'm getting an error "circluar dependencies cannot exist in Relative layout"

heres what im trying to acheive in termas of layout and then fill each text field with data.

date                number
name   score - score  name
scorers              scorers

heres my attempt

<?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="wrap_content"
    android:background="@color/white"
    android:paddingTop="30dp"
    android:paddingBottom="30dp" >



    <TextView
        android:id="@+id/TextView04"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_below="@+id/HomeScore"
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        android:text="-"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/AwayTeam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView04"
        android:layout_alignBottom="@+id/TextView04"
        android:layout_alignParentRight="true"
        android:layout_marginRight="22dp"
        android:gravity="left"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/HomeTeam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView04"
        android:layout_alignBottom="@+id/TextView04"
        android:layout_toRightOf="@+id/HomeScore"
        android:gravity="right"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Attendance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/AwayTeam"
        android:layout_alignTop="@+id/HomeScore"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/Division"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/HomeScore"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/AwayScore"
        android:layout_width="20dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/TextView04"
        android:layout_alignBottom="@+id/TextView04"
        android:layout_toRightOf="@+id/TextView04"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/HomeScore"
        android:layout_width="20dp"
        android:layout_height="wrap_content"

        android:layout_alignBottom="@+id/TextView04"
        android:layout_marginRight="15dp"
        android:layout_toLeftOf="@+id/TextView04"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />

</RelativeLayout>

heres the error

06-06 14:42:44.348: E/AndroidRuntime(1167): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1272)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:281)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:303)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.view.View.measure(View.java:8526)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.ListView.measureScrapChild(ListView.java:1153)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1266)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.ListView.onMeasure(ListView.java:1127)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.view.View.measure(View.java:8526)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3224)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.TableLayout.measureChildBeforeLayout(TableLayout.java:456)
06-06 14:42:44.348: E/AndroidRuntime(1167):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)

回答1:

Well, you have a few problems here:

  1. TextView04 declares android:layout_below="@+id/HomeScore" and android:layout_centerVertical="true", which conflict

  2. AwayTeam declares android:layout_alignBaseline="@+id/TextView04" and android:layout_alignBottom="@+id/TextView04", which conflict

  3. HomeTeam declares android:layout_alignBaseline="@+id/TextView04" and android:layout_alignBottom="@+id/TextView04", which conflict

  4. AwayScore declares android:layout_alignBaseline="@+id/TextView04" and android:layout_alignBottom="@+id/TextView04", which conflict

As Samir points out, at least one of your circular dependencies is between TextView04 and HomeScore. TextView04 says it is to be below HomeScore, and HomeScore says its bottom is aligned with the bottom of TextView04. However, some of the other ones I note above may also create circular dependencies. Ensure that two widgets do not try to constrain on each other on the same axis (as in TextView04 and HomeScore).



回答2:

Remove android:layout_below="@+id/HomeScore" from first TextView in your layout it creates circular dependencies.

Why ?

Because you set android:layout_alignBottom="@+id/TextView04" in Textview whose id is android:id="@+id/HomeScore".and first Textview(TextView04) have also android:layout_below="@+id/HomeScore".

 <TextView
        android:id="@+id/TextView04"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true" 
        android:layout_centerHorizontal="true" 
        android:text="-"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/blue"
        android:textStyle="bold" />


回答3:

You have a small mistake that might be the reason:

When you reference to another id, don't use @+id/... just @id/.... So everywhere except the android:id attribute the + must be removed.

After you have done that you might get some warnings that point to the possible error!