Android Relative Layout循环依赖项错误

时间:2012-06-06 15:01:37

标签: android layout

嗨我试图创建一个自定义布局,但我正在努力让它正常工作我得到一个错误“相对布局中不存在circluar依赖”

下面是我试图在布局中创建的内容,然后用数据填充每个文本字段。

date                number
name   score - score  name
scorers              scorers

继承人我的尝试

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

继承错误

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)

3 个答案:

答案 0 :(得分:13)

嗯,这里有一些问题:

  1. TextView04声明android:layout_below="@+id/HomeScore"android:layout_centerVertical="true",发生冲突

  2. AwayTeam声明android:layout_alignBaseline="@+id/TextView04"android:layout_alignBottom="@+id/TextView04",发生冲突

  3. HomeTeam声明android:layout_alignBaseline="@+id/TextView04"android:layout_alignBottom="@+id/TextView04",发生冲突

  4. AwayScore声明android:layout_alignBaseline="@+id/TextView04"android:layout_alignBottom="@+id/TextView04",发生冲突

  5. 正如Samir所指出的,至少有一个循环依赖项介于TextView04HomeScore之间。 TextView04表示它低于HomeScore,而HomeScore表示其底部与TextView04的底部对齐。但是,我上面提到的其他一些也可能会产生循环依赖。确保两个小部件不会尝试在同一轴上相互约束(如TextView04HomeScore)。

答案 1 :(得分:7)

从布局中的第一个android:layout_below="@+id/HomeScore"移除TextView,它会创建循环依赖关系。

  

为什么?

因为您在Textview中设置了android:layout_alignBottom="@+id/TextView04",其ID为 android:id="@+id/HomeScore"。第一个Textview(TextView04)也有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" />

答案 2 :(得分:1)

你可能有一个小错误:

当您引用其他ID时,请不要仅使用@+id/... @id/...。因此除了android:id属性之外的任何地方都必须删除+

完成后,您可能会收到一些指示可能错误的警告!