我正在将 View 渲染为位图
。我需要一个顶部或底部带有指针的气球,具体取决于它是否适合屏幕。为此,我使用 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 ,然后我想测量和布局它,这样我就可以将它渲染成位图:
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 em>-s 但没有成功。如果我删除最后一个 ImageView 中的 android:layout_below="@id/balloon_body" ,它可以工作,但底部指针位于顶部。
我无法使用 LinearLayout,因为我需要这些指针位于气球主体上方,而使用 LinearLayout 时,顶部指针将位于气球主体下方。问题出在哪里,我怎样才能达到我想要的效果?
最佳答案
该问题是由于布局参数中存在循环引用而引起的。
例如,当 View B是layout_below View A时, View A不能再在其下方引用 View B,alignRight等。这也可以存在于多个 View 之间:A引用B引用C。在这种情况下C可以'由于循环依赖,t 引用 A。
有关更多信息,请访问:Circular dependencies cannot exist in RelativeLayout, android?
希望有帮助!
关于android - 循环依赖不能存在于RelativeLayout中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14157358/