RelativeLayout中不存在循环依赖关系

时间:2013-01-04 12:38:11

标签: android relativelayout

我正在将视图呈现为Bitmap。根据它是否适合屏幕,我需要一个顶部或底部有指针的气球。为此,我使用RelativeLayout,因为我需要指针部分地在气球上:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <LinearLayout android:id="@+id/balloon_body"
        android:layout_width="@dimen/pins_details_balloon_width"
        android:layout_height="wrap_content"
        android:layout_marginTop="-1dp"
        android:layout_marginBottom="-1dp"
        android:layout_below="@+id/pointer_top"
        android:orientation="vertical"
        >

        <TextView android:id="@android:id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            />

        <TextView android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            />

    </LinearLayout>

    <ImageView android:id="@id/pointer_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <ImageView android:id="@+id/pointer_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/balloon_body"
        />

</RelativeLayout>

我给这个视图充气,然后我想测量和布局它,所以我可以将它渲染成位图:

view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
//view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//view.measure(View.MeasureSpec.makeMeasureSpec(3000, View.MeasureSpec.AT_MOST), View.MeasureSpec.makeMeasureSpec(3000, View.MeasureSpec.AT_MOST));
view.layout(0, 0, s_View.getMeasuredWidth(), s_View.getMeasuredHeight());

此代码在 view.measure(...)上崩溃,在RelativeLayout 错误中不存在循环依赖关系。我尝试了不同的 LayoutParams (没有setLayoutParams(...) measure(...)与空指针异常崩溃),不同的 View.MeasureSpec -s但没有成功。如果我删除了最后一个ImageView中的 android:layout_below =“@ id / balloon_body”,它可以正常工作,但是底部指针位于顶部。

我无法使用LinearLayout,因为我需要这些指针位于气球体上方,而LinearLayout顶部指针将位于气球体下方。问题在哪里,我怎样才能达到我的目的?

4 个答案:

答案 0 :(得分:3)

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

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

有关详细信息,请转到:Circular dependencies cannot exist in RelativeLayout, android?

希望它有帮助!

答案 1 :(得分:0)

试试这个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

 <ImageView android:id="@id/pointer_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<LinearLayout android:id="@+id/balloon_body"
    android:layout_width="@dimen/pins_details_balloon_width"
    android:layout_height="wrap_content"
    android:layout_marginTop="-1dp"
    android:layout_marginBottom="-1dp"
    android:layout_below="@+id/pointer_top"
    android:orientation="vertical"
    >

    <TextView android:id="@android:id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        />

    <TextView android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        />

</LinearLayout>
<ImageView android:id="@+id/pointer_bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/balloon_body"
    />

答案 2 :(得分:0)

如果您在相对布局中使用圆形背景(可绘制形状),则会抛出该错误

删除该形状或更改布局

答案 3 :(得分:0)

循环依赖项是指两个连续的视图,它们在位置上相互依赖。

例如:

 <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs"
    android:layout_above="@id/orderpaylayout"
    />

<Button
    android:id="@+id/orderbutton"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:layout_centerInParent="true"
    android:background="@drawable/orderbuttonstyle"
    android:text="@string/order"
    android:textSize="16sp"
    android:textStyle="bold"
    android:visibility="gone" />

<Button
    android:id="@+id/paybutton"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_centerInParent="true"
    android:text="@string/Pay"
    android:textColor="#ffffff"
    android:textSize="16sp"
    android:visibility="gone"
    android:layout_below="@id/viewPager
    android:textStyle="bold"
    android:layout_margin="20dp"
    android:background="@drawable/paybuttonstyle"
    />

在上面的XML中,有三个视图。 First View ViewPager取决于订单按钮,其为 layout_above = orderButton 。一旦顶视图或底视图依赖于其他视图,就不会发生向后/向后依赖性,即,两个视图不能同时相互依赖

上述错误示例的正确XML代码是:

 <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tabs"
    android:layout_above="@id/orderpaylayout"
    />

<RelativeLayout
    android:id="@+id/orderpaylayout"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content">

<Button
    android:id="@+id/orderbutton"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="20dp"
    android:layout_centerInParent="true"
    android:background="@drawable/orderbuttonstyle"
    android:text="@string/order"
    android:textSize="16sp"
    android:textStyle="bold"
    android:visibility="gone" />

<Button
    android:id="@+id/paybutton"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_centerInParent="true"
    android:text="@string/Pay"
    android:textColor="#ffffff"
    android:textSize="16sp"
    android:visibility="gone"
    android:textStyle="bold"
    android:layout_margin="20dp"
    android:background="@drawable/paybuttonstyle"
    />
</RelativeLayout>

希望,本示例将为您提供帮助。如果您认为这是正确的方法,赞成他人的利益,或者如果它是错误的或不起作用,请发表您的评论。

相关问题