英文:
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 ("
, <
, >
, 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.
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
<RelativeLayout
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>
答案1
得分: 1
不要使用show_message TextView
的文本消息背景,而是将msg TextView
和date 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>
英文:
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 "@drawable/background_right"
on that container.
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论