所以我的文本视图必须绘制在图像视图上,所以它在 xml 中定义如下:
<ImageView
android:id="@+id/chatBalloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/chatItemProfPic"
android:scaleType="fitXY"
android:src="@drawable/chat_bar_user" />
<TextView
android:id="@+id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/chatItemProfPic"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
但我因为 textView 可以包含多行文本,所以我需要让 imageView 相应地增加它的高度。这将通过添加此规则来完成:
android:layout_alignBottom="idOfText"
但由于该部分尚未定义 textView,因此应用程序崩溃。当尝试通过 LayoutParams 中的 addRule 代码执行此操作时,我得到了相同的结果,因为我在绘制视图之前在 onCreate 中调用它。
任何想法如何绕过这个?
已解决:最终 xml:
<ImageView
android:id="@+id/chatBalloon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/chatItemProfPic"
android:scaleType="fitXY"
android:layout_alignBottom="@+id/userText"
android:src="@drawable/chat_bar_user" />
<TextView
android:id="@id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="7dp"
android:layout_marginTop="3dp"
android:layout_toRightOf="@+id/chatItemProfPic"
android:text="username"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />