简体   繁体   中英

Textview comes on top Imageview

I'm trying to add a TextView and an ImageView in a RelativeLayout , as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">


        <TextView
            android:background="@drawable/bg_message2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
            android:textSize="16dp"
            android:id="@+id/mUser"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mUserImg"
            android:layout_alignParentRight="true"
            android:background="@drawable/bg_profile_message"/>

</RelativeLayout>

The above code looks like this: http://i.stack.imgur.com/5vhsc.png

and What I want : http://i.stack.imgur.com/PVDIK.jpg

how do I prevent the TextView being underlaid on the ImageView ?

First set the ImageView in place, then set the TextView.
You can give the TextView a width of "match_parent", because it will fill just the remaining space.

So:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mUserImg"
        android:layout_alignParentRight="true"
        android:background="@drawable/bg_profile_message"
    />
    <TextView
        android:background="@drawable/bg_message2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/mUserImg"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
        android:textSize="16dp"
        android:id="@+id/mUser"
    />
</RelativeLayout>

Modify like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:paddingTop="10dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">


        <TextView
            android:background="@drawable/bg_message2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_alignParentLeft="true"
            android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
            android:textSize="16dp"
            android:id="@+id/mUser"/>
        <ImageView
            android:layout_width="match_parent"
            android:layout_weight="0.8"
            android:layout_height="wrap_content"
            android:id="@+id/mUserImg"
            android:layout_alignParentRight="true"
            android:background="@drawable/bg_profile_message"/>

</LinearLayout>

You can reduce and increase the value of layout_weight depending on your needs

Hope it helps!!!

<?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"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/mUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/mUserImg"
        android:background="@drawable/bg_message2"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/mUserImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
         android:background="@drawable/bg_profile_message" />

</RelativeLayout>

You can with this for your TextView :

layout_alignParentLeft="true" 

and this for right side for your ImageView :

android:layout_alignParentRight="true"

EDIT

And you can add this for your TextView :

layout_toLeftOf="@+id/mUserImg"

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