Need for message text and time retrieved from Firebase to be of different sizes, but end up in same TextView

huangapple go评论74阅读模式
英文:

Need for message text and time retrieved from Firebase to be of different sizes, but end up in same TextView

问题

// MessageAdapter

holder.show_message.setText(chat.getMessage());
holder.message_time.setTextSize(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.xml -->

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

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

Note: I've removed the HTML-like formatting (&quot;, <, >, etc.) to make the code snippets more readable. Please use the provided translations directly in your code.

英文:

I am creating a chat app and each individual message should have its respective text and time. The issue is that I want the message time to be a different size than the message text, but they should each fall into the same spot in the layout with their respective background.

At first I created the time below the message in the xml file as you can see in the picture, but I don't like it there. I want it inside of the message background. So programmatically I am trying to retrieve and change the size of the message time, but both should go in the same holder.show_message, but both text and time are coming back the same size.

From the information I have found online I have been doing it like this, but this isn't giving me the desired result. The time below the message I am going to get rid of, just wanted to show the text format of how I want the time Inside of the message to be.

**Need for message text and time retrieved from Firebase to be of different sizes, but end up in same TextViewMessageAdapter**

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

chat_item_right

<RelativeLayout

xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:padding=&quot;8dp&quot;&gt;

    &lt;TextView
        android:id=&quot;@+id/date&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginBottom=&quot;25dp&quot; /&gt;

    &lt;RelativeLayout
        android:layout_width=&quot;300dp&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@+id/date&quot;
        android:layout_alignParentEnd=&quot;true&quot;&gt;

        &lt;de.hdodenhof.circleimageview.CircleImageView
            android:id=&quot;@+id/profile_image&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:visibility=&quot;gone&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/show_message&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_alignParentEnd=&quot;true&quot;
            android:background=&quot;@drawable/background_right&quot;
            android:padding=&quot;10dp&quot;
            android:text=&quot;Okay so let&#39;s start experimenting&quot;
            android:textColor=&quot;#000&quot;
            android:textSize=&quot;18sp&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/message_time&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@+id/show_message&quot;
            android:layout_alignStart=&quot;@id/show_message&quot;
            android:layout_marginStart=&quot;10dp&quot;
            android:text=&quot;12:26&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/text_seen&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@+id/show_message&quot;
            android:layout_alignParentEnd=&quot;true&quot; /&gt;

    &lt;/RelativeLayout&gt;

&lt;/RelativeLayout&gt;

答案1

得分: 1

不要使用show_message TextView的文本消息背景,而是将msg TextViewdate TextView与一个容器视图(例如LinearLayout)包围起来,并在该容器上设置&quot;@drawable/background_right&quot;的背景。

<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>
英文:

Instead of assigning your text message background for the show_message TextView, surround your msg TextView and your date TextView with a container view (in my example LinearLayout) and set the background of &quot;@drawable/background_right&quot; on that container.

 &lt;LinearLayout
    android:orientation=&quot;vertical&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:background=&quot;@drawable/background_right&quot;&gt;

&lt;TextView
    android:id=&quot;@+id/show_message&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:gravity=&quot;right&quot;
    android:padding=&quot;10dp&quot;
    android:text=&quot;Okay so let&#39;s start experimenting&quot;
    android:textColor=&quot;#000&quot;
    android:textSize=&quot;18sp&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/message_time&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:gravity=&quot;left&quot;
    android:layout_marginStart=&quot;10dp&quot;
    android:text=&quot;12:26&quot; /&gt;

&lt;TextView
    android:id=&quot;@+id/text_seen&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:gravity=&quot;right&quot; /&gt;

&lt;/LinearLayout&gt;

huangapple
  • 本文由 发表于 2020年4月4日 01:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定