1

这是我的捕获图片:
在此处输入图像描述

这是我的代码:

<RelativeLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="left">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"  
        android:layout_toRightOf="@+id/messageIn"          
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />



</RelativeLayout>

但是,如果文本“HIHIHIHIHIHI”更改为“HIHIHIHIHIHIHIHIHIHIHIHIHIHIHIHIHI”:时间将丢失,气泡将填充父项
在此处输入图像描述

即使文本是多行的,我该怎么做才能使时间仍然在气泡的右侧?

4

5 回答 5

2

尝试这个

<RelativeLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:gravity="left">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"           
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:layout_alignParentRight="true"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:layout_toLeftOf="@+id/timeIn" 
        android:layout_alignParentLeft="true" 
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</RelativeLayout>

也适用于小文本(根据消息调整 messageIn 的宽度)和出现在消息右侧的时间(不是右对齐)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1.0" >

<LinearLayout
    android:id="@+id/lin_lay1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.0" >

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHI" />
</LinearLayout>

<LinearLayout
    android:id="@+id/lin_lay2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.1" >

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:padding="5dp"
        android:text="23:23" />
</LinearLayout>

</LinearLayout>
于 2012-10-25T04:45:03.457 回答
1

在你的文本视图中使用 android:maxwidth。

于 2012-10-25T04:41:42.597 回答
1

一个解决方案是设置“messageIn”TextView 的 android:maxWidth="" 属性。在运行时 - 获取设备屏幕的宽度,“timeIn”TextView 的宽度,并使用 TextView 的 setMaxWidth(int maxpixels) 方法设置差异(屏幕宽度 - timeIn 宽度)。

在此处查看如何获取屏幕尺寸

在此处查看如何获取 GUI 对象大小

于 2012-10-25T04:48:15.393 回答
0

使用 layout_weight 它只会占用分配的空间,并且不会覆盖时间 textview。

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:weightsum="1">

    <TextView
        android:id="@+id/timeIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"  
        android:layout_weight="0.7"
        android:layout_gravity="center"
        android:layout_marginTop="15dip"
        android:text="23:23"/>

    <TextView
        android:id="@+id/messageIn"
        android:layout_width="0dip"
        android:layout_height="wrap_content"       
        android:layout_weight="0.3"
        android:layout_margin="5dip"
        android:background="@drawable/bubble_yellow"
        android:paddingLeft="10dip"
        android:text="HIHIHIHIHIHI" />

</LinearLayout>
于 2012-10-25T04:37:24.143 回答
0

你可以通过两种方式做到这一点: -

  1. 您可以使用线性布局并设置其 android:orientation="horizo​​ntal" 并且您需要修复 Textview 的 layout_width。

  2. 在代码中将时间与 TextView 连接起来。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">
     <TextView
     android:id="@+id/messageIn"
     android:layout_width="200dp"
     android:layout_height="wrap_content"      
     android:text="HIHIHIHIdsfffffffffffffffffffffffffffffffffffffffffffffHIHI" />
    <TextView
     android:id="@+id/timeIn"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"  
     android:layout_marginTop="5dp"
     android:text="23:23"/>
    
    
     </LinearLayout>
    
于 2012-10-25T04:47:37.143 回答