gpt4 book ai didi

android - RelativeLayout 匹配父级,虽然它不应该

转载 作者:行者123 更新时间:2023-11-29 23:30:55 25 4
gpt4 key购买 nike

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

此消息布局应包含:

  • 圆角矩形中的消息 TextView
  • 同一矩形内的消息状态检查图标 ImageView
  • 消息发送时间 TextView 在圆角矩形的右侧。

当文本很短时,整个布局应该靠右对齐。当文本很长时,消息 TextView 会向左扩展,当触摸屏幕左侧时,它会扩展到新的一行。

问题是:尽管底层 FrameLayout 宽度设置为 wrap_content,但无论消息是短还是长,圆角矩形都会延伸到整个屏幕。

这是问题的图片:

enter image description here

我使用的布局是:

<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_ll 的内部 RelativeLayout 的宽度等于 wrap_content

我期待的是:

  • text_message_time TextView 应附加到屏幕右侧,因为其 layout_alignParentEnd 属性设置为 true。
  • bubble_ll 由于其 layout_toLeftOf 属性集而位于 text_message_time View 的左侧。它的内容应该有一个宽度。

我尝试过其他布局,例如 Linear、ConstraintLayout 等。

LinearLayout 方法不起作用,因为宽度设置为 wrap_content 对于 TextView 总是重叠状态信息检查和消息时间使它们当 TextView 中的消息很长时消失。

使用 ConstraintLayout 我无法将气泡对齐以保持在右侧。它水平地停留在屏幕的中心,因为它在屏幕的两个水平边上都受到约束。

我们将不胜感激。

最佳答案

我想出了以下布局,它是 RelativeLayoutLinearLayout 的组合。我希望这能达到你的目的。如有必要,请更改可绘制对象和 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>

关于android - RelativeLayout 匹配父级,虽然它不应该,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52684960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com