Android RelativeLayout匹配父项,尽管它不应该';T

Android RelativeLayout匹配父项,尽管它不应该';T,android,textview,android-relativelayout,android-wrap-content,Android,Textview,Android Relativelayout,Android Wrap Content,我正在尝试在聊天应用程序中创建消息布局 此消息布局应包含: 圆角矩形中的消息文本视图 同一矩形内的消息状态检查图标ImageView 消息发送时间TextView位于圆角矩形的右侧 当文本较短时,整个布局应向右对齐。 当文本较长时,messageTextView将向左扩展,触摸屏幕左侧时,它将扩展到新行 问题在于:圆形矩形延伸到整个屏幕,不管消息是短消息还是长消息,尽管底层的FrameLayoutwidth设置为包裹内容 以下是问题的图像: 我使用的布局是: <RelativeLay

我正在尝试在聊天应用程序中创建消息布局

此消息布局应包含:

  • 圆角矩形中的消息文本视图
  • 同一矩形内的消息状态检查图标
    ImageView
  • 消息发送时间
    TextView
    位于圆角矩形的右侧
当文本较短时,整个布局应向右对齐。 当文本较长时,message
TextView
将向左扩展,触摸屏幕左侧时,它将扩展到新行

问题在于:圆形矩形延伸到整个屏幕,不管消息是短消息还是长消息,尽管底层的
FrameLayout
width设置为包裹内容

以下是问题的图像:

我使用的布局是:

<RelativeLayout
    android:id="@+id/outer_rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingTop="8dp">

    <TextView
        android:id="@+id/text_message_time"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="4dp"
        android:textSize="10sp"
        app:showDate="@{message.postedAt}" />

    <RelativeLayout
        android:id="@+id/bubble_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/text_message_time"
        android:background="@drawable/rounded_rectangle_bubble_sent"
        android:padding="8dp">

        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="4dp"
            app:setStatusPng="@{message.status}" />

        <TextView
            android:id="@+id/messagetext_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="2dp"
            android:layout_toLeftOf="@id/iv_check"
            android:singleLine="false"
            android:text="@{message.messageText}"
            android:textColor="#ffffff" />

    </RelativeLayout>
</RelativeLayout>

名为
bubble\u ll
的内部
RelativeLayout
的宽度等于
wrap\u content

我期待的是:

  • 文本\u消息\u时间文本视图应附加到屏幕右侧,因为其布局\u alignParentEnd属性设置为true
  • bubble_ll位于文本消息时间视图的左侧,因为它的layout_toLeftOf属性集。它应该有一个宽度的内容
我尝试过其他布局,如线性、约束等

LinearLayout
方法不起作用,因为
TextView
的宽度设置为
wrap\u content
,总是与状态信息检查重叠,当
TextView
中的消息较长时,消息时间会使它们消失

使用
ConstraintLayout
时,我无法对齐气泡使其保持静止在右侧。它水平地停留在屏幕的中心,因为它被限制在屏幕的水平两侧


任何帮助都将不胜感激。

我提出了以下布局,它是
相对布局
线性布局
的组合。我希望这能达到你的目的。如有必要,请更换抽绳和ID

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outer_rl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:padding="8dp">

    <TextView
        android:id="@+id/text_message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/message_container"
        android:layout_alignParentRight="true"
        android:layout_margin="4dp"
        android:text="10:30 am"
        android:textSize="10sp" />

    <LinearLayout
        android:id="@+id/message_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"

        android:layout_toLeftOf="@+id/text_message_time"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/messagetext_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:layout_weight="1"
            android:padding="4dp"
            android:singleLine="false"
            android:text="You have a long message here. This is so long that it takes two lines inside the TextView. No wonder, now its taking three lines and now its four. How long this message can go? I now have that question!!!"
            android:textColor="@android:color/white" />

        <ImageView
            android:id="@+id/iv_check"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            android:src="@android:drawable/presence_online" />
    </LinearLayout>
</RelativeLayout>


您不应该再使用相对布局。。使用约束布局。您的约束布局问题可以通过约束布局的水平偏移来解决。使用9块图像作为消息的背景。不是布局。@ArchieG.Quiñones带有约束布局水平偏差,我不知道应该设置什么作为偏差。我希望文本始终右对齐。但同时,当文本太长时,当文本视图的左侧接触屏幕左侧时,文本视图扩展必须停止。那么我应该将其设置为0.9吗?我更喜欢使用约束布局,但无法按照我想要的方式进行操作。如果你能帮我,我会很高兴的。@UmangBurman你看,信息框里还有一个ImageView,所以我想我需要一个容器。问题是容器的宽度在某种程度上与parrent匹配。我用作背景的可绘制文件仅用于绘制角点。当您正确设置约束时,无需考虑您所谈论的所有事情。要将文本向右对齐,只需将偏差设置为1.0。不幸的是,正如我在问题中提到的,文本视图是包装内容,当文本太长时,它会占据ImageView空间。当文本较长时,图像视图不再显示在气泡内。