Textview 排在前面 Imageview

Textview comes on top Imageview

我正在尝试在 RelativeLayout 中添加一个 TextView 和一个 ImageView,如下所示

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

上面的代码是这样的:

和我想要的:

如何防止 TextView 被覆盖在 ImageView 上?

先设置ImageView,再设置TextView。
您可以为 TextView 指定 "match_parent" 的宽度,因为它只会填充 剩余的 space.

所以:

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

修改如下:

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

您可以根据需要减少和增加layout_weight的值

希望对您有所帮助!!!

您可以将此用于您的 TextView :

layout_alignParentLeft="true" 

这是您的 ImageView 的右侧:

android:layout_alignParentRight="true"

编辑

您可以为您的 TextView 添加这个:

layout_toLeftOf="@+id/mUserImg"
<?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>