【问题标题】:How to create chat message block like WhatsApp in android?如何在 android 中创建像 WhatsApp 这样的聊天消息块?
【发布时间】:2015-01-21 19:12:49
【问题描述】:

我正在尝试创建类似 WhatsApp 的聊天布局。我创建了带有消息和时间的块消息项目布局。该块的最大宽度必须是屏幕的 80%。如果消息 textView 小于块宽度,则消息 textView 和时间 textView 必须在一行中。如果消息 textView 超过块宽度,则它必须转到另一行,并且此行的结尾必须是 time textView。但我正在失去时间视图。我必须做什么? 这是我的布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="right|top">
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:layout_alignParentRight="true" android:layout_alignParentEnd="true"
            android:layout_margin="10dp" android:background="#eee" android:padding="5dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                android:id="@+id/messageView" android:layout_marginRight="10dp"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10:52"
                android:id="@+id/timeView" android:textColor="#999" android:textSize="10dp"/>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

标签: android layout textview whatsapp


【解决方案1】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="right|top">
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:layout_alignParentRight="true" android:layout_alignParentEnd="true"
            android:layout_margin="10dp" android:background="#eee" android:padding="5dp">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                android:id="@+id/messageView" android:layout_marginRight="40dp"/>
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10:52"
                android:id="@+id/timeView" android:textColor="#999" android:textSize="10dp"
                android:layout_marginLeft="-30dp" android:layout_gravity="bottom"/>
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 它帮不上忙。因为当 messageView 超过 1 行时,timeView 的上边会是空的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多