在TextView下面的Android XML ImageView,但需要与textView对齐底部

时间:2013-08-23 11:11:46

标签: android xml layout

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

2 个答案:

答案 0 :(得分: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" />

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

答案 1 :(得分: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上