文本视图在图像视图上方

3
我正在尝试将TextViewImageView添加到RelativeLayout中,如下所示。
<?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>

以上代码的效果如下:http://i.stack.imgur.com/5vhsc.png 我想要的效果是:http://i.stack.imgur.com/PVDIK.jpg 如何避免TextViewImageView覆盖?

@helldawg13 权重只在LinearLayout中起作用。 - Phantômaxx
4个回答

1

首先将ImageView放置在指定位置,然后设置TextView。
你可以给TextView设置宽度为“match_parent”,因为它会填充剩余的空间。

所以:

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

1
当我添加 layout_toLeftOf="@id/mUserImg" 时,它可以工作。非常感谢。 - Muhammet Demir

0

修改为:

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

希望能对您有所帮助!!!


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

在 ImageView 中删除了双重的 background 属性。 - Phantômaxx

-1
你可以使用这个来处理TextView:
layout_alignParentLeft="true" 

这是针对您的ImageView右侧的内容:

android:layout_alignParentRight="true"

编辑

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

layout_toLeftOf="@+id/mUserImg"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接