0

所以我的文本视图必须绘制在图像视图上,所以它在 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" />
4

2 回答 2

2

你用

 android:layout_alignBottom="@+id/idOfText"

当您第一次引用特定 ID 时。请注意将 id 添加到资源标识符列表中的“+”。

然后,您可以在不使用“+”的情况下将现有 id 分配给小部件,如下所示:

android:id="@id/idOfText"

后。通常在我们分配 id 时创建它,这就是为什么你只需要关心相对布局中“+”的存在。

在您的具体情况下:

<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:layout_alignBottom="@+id/userText"
    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" />

您应该使用以下方法设置配置文件图片小部件的 ID:android:id = "@+id/chatItemProfPic"假设您在对其进行任何引用之前声明小部件。否则,类似地,使用“+”作为第一个引用,然后在声明不带“+”的小部件时分配 ID。

于 2013-08-23T11:14:32.460 回答
0

为什么不把那个图像像textview背景一样。?

或类似的东西:

<RelativeLayout
            android:id="@+id/Rlayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="35dp" >

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/img" />

            <TextView
                android:id="@+id/text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:text="lorem ipsum"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textSize="@dimen/dimension" />
        </RelativeLayout>

在相对布局中,您可以轻松地将文本放在 imageview 上

于 2013-08-23T11:17:59.763 回答