java - 需要从 Firebase 检索的消息文本和时间具有不同的大小,但最终位于相同的 TextView 中

标签 java android

我正在创建一个聊天应用程序,每条消息都应该有其各自的文本和时间。问题是我希望消息时间的大小与消息文本的大小不同,但它们应该各自落入布局中具有各自背景的同一位置。

首先,我在 xml 文件中的消息下方创建了时间,如图所示,但我不喜欢它。我希望它位于消息背景内。因此,我试图以编程方式检索和更改消息时间的大小,但两者都应位于相同的 holder.show_message 中,但文本和时间都会返回相同的大小。

根据我在网上找到的信息,我一直在这样做,但这并没有给我想要的结果。我要删除的消息下方的时间只是想显示我希望消息内的时间的文本格式。

<强> enter image description here消息适配器

holder.show_message.setText(chat.getMessage() + "\n" + String.format(chat.getTime(), TypedValue.COMPLEX_UNIT_SP, 10));
        if (imageurl.equals("default")) {
            holder.profile_image.setImageResource(R.mipmap.ic_launcher);
        } else {
            Glide.with(mContext).load(imageurl).into(holder.profile_image);
        }

chat_item_right

xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="8dp">

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp" />

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date"
        android:layout_alignParentEnd="true">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <TextView
            android:id="@+id/show_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:background="@drawable/background_right"
            android:padding="10dp"
            android:text="Okay so let's start experimenting"
            android:textColor="#000"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/show_message"
            android:layout_alignStart="@id/show_message"
            android:layout_marginStart="10dp"
            android:text="12:26" />

        <TextView
            android:id="@+id/text_seen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/show_message"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</RelativeLayout>

最佳答案

不要为 show_message TextView 分配短信背景,而是用容器 View 包围 msg TextViewdate TextView(在我的示例 LinearLayout) 并在该容器上设置 "@drawable/background_right" 的背景。

 <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_right">

<TextView
    android:id="@+id/show_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:padding="10dp"
    android:text="Okay so let's start experimenting"
    android:textColor="#000"
    android:textSize="18sp" />

<TextView
    android:id="@+id/message_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:layout_marginStart="10dp"
    android:text="12:26" />

<TextView
    android:id="@+id/text_seen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right" />

</LinearLayout>

关于java - 需要从 Firebase 检索的消息文本和时间具有不同的大小,但最终位于相同的 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61017276/

相关文章:

java - 防止在 selenium webdriver 测试中加载外部内容

java - Spring Boot @GetMapping 到以 .jsp 结尾的 URL

android - 奇怪的 ListView 项目选择器外观(手指离开屏幕时未移除按下状态)

android - 如何以易于维护的格式组合图像、字符串和整数集

android - 在 Android 中动态更新自动完成框?

Java SSL : how to get client certificate information

java - 以下 ^.*$ 正则表达式匹配什么?

java - 当 @Autowired 和 @Bean 出现在同一个类中时出现 BeanCurrentlyInCreationException

java - 唤醒锁和 wifiLock

android - 尝试使用 firebase auth 设置确认密码