简体   繁体   中英

Android XML ImageView below TextView but needs to align bottom with textView

So My text View has to be drawn over the image view, so its defined in xml like this:

    <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" />

but i because the textView can contain multiline text, i need to have the imageView increase its height accordingly. It would be accomplished by adding this rule:

android:layout_alignBottom="idOfText"

but because the textView hasnt been defined at that part, the app crashes. I get the same when trying to do it from code by addRule in the LayoutParams because i call it in onCreate, before the view has been drawn.

Any ideas how to bypass this?

SOLVED: Final 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" />

You use

 android:layout_alignBottom="@+id/idOfText"

when you first make a reference to a particular id. Note the "+" which is adding the id to the list of resource identifiers.

Then, you can assign an existing id to a widget without using the "+", as follows:

android:id="@id/idOfText"

after. It's typical to create the id when we're assigning it, which is why you only really need to care about the presence of the "+" in relative layouts.

In your specific case:

<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" />

You should have set the ID of the profile pic widget using: android:id = "@+id/chatItemProfPic" assuming that you are declaring the widget before any references to it. Otherwise, similarly, use "+" for the first reference, and then assign the ID when you declare the widget without the "+".

why dont put that image like textview background.?

or something like:

<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>

in relative layout you could easily put text right over imageview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM