android - 备用聊天气泡宽度

标签 android android-layout

我正在开发一个聊天类型的应用程序。我正在使用两个不同的 9-patch 来处理聊天气泡主要消息和响应。我面临的问题是根据消息长度自动包装气泡的宽度。以下是我的主要布局:

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    >

 <ListView android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:cacheColorHint="#00000000"
    android:listSelector="@android:color/transparent"
    android:divider="@null"
    />
 </RelativeLayout>

<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:gravity="bottom" style="@android:style/ButtonBar">
    <Button
        android:id="@+id/stt_button"
        android:layout_width="45dp"
        android:layout_height="50dp"
        android:background="@drawable/microphone"
    />   

    <EditText android:id="@+id/chat_msg"
        android:inputType="text" 
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_weight="1" />

    <Button android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:text="@string/send_button" />
</LinearLayout>
</LinearLayout>

这是我的 list_item 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content" android:weightSum="1.0"
android:layout_weight="1" android:layout_height="wrap_content">
<LinearLayout         
    android:id="@+id/linearLayoutLeft" 
    android:layout_width="0dp"
    android:layout_weight="0.8"
    android:layout_height="wrap_content">
    <RelativeLayout 
        android:id="@+id/relativeLayoutLeft" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/lefttext" 
            android:layout_width="wrap_content" 
            android:gravity="top" 
            android:layout_height="wrap_content" 
            android:paddingLeft="10dip" 
            android:paddingRight="10dip" 
            android:paddingTop="5dip" 
            android:layout_alignParentLeft="true">
        </TextView>
    </RelativeLayout>
</LinearLayout>
<LinearLayout         
    android:id="@+id/linearLayoutRight"
    android:layout_width="0dp"         
    android:layout_weight="0.8"
    android:layout_height="wrap_content">
    <RelativeLayout 
        android:id="@+id/relativeLayoutRight" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
        <TextView 
            android:id="@+id/righttext" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:paddingLeft="10dip" 
            android:paddingRight="10dip" 
            android:paddingTop="5dip" 
            android:layout_alignParentRight="true" 
            android:layout_alignParentTop="true">
        </TextView>
    </RelativeLayout>
</LinearLayout>

这是我的自定义数组适配器的 getView 方法中的代码:

    View view = convertView;
    if(view == null){
        view = mInflater.inflate(R.layout.list_item, null);
    }

    Resources res = getContext().getResources();
    Drawable bubblesChat = res.getDrawable(R.drawable.bubbles_chat);
    Drawable bubblesResponse = res.getDrawable(R.drawable.bubbles_response);
    TextView left = (TextView) view.findViewById(R.id.lefttext);
    TextView right = (TextView) view.findViewById(R.id.righttext);
    View leftLayout = view.findViewById(R.id.relativeLayoutLeft);
    View rightLayout = view.findViewById(R.id.relativeLayoutRight); 
    LinearLayout linearLeft = (LinearLayout) view.findViewById(R.id.linearLayoutLeft);
    LinearLayout linearRight = (LinearLayout) view.findViewById(R.id.linearLayoutRight);

    LayoutParams leftParams = (LayoutParams) linearLeft.getLayoutParams();
    LayoutParams rightParams = (LayoutParams) linearRight.getLayoutParams();

    String txt = super.getItem(position);
    if(txt.startsWith("s:")) {
        left.setText(getItem(position));
        leftLayout.setBackgroundDrawable(bubblesChat);
        leftParams.weight = 0.8f;
        linearLeft.setLayoutParams(leftParams);
        rightParams.weight = 0.2f;
        linearRight.setLayoutParams(rightParams);
        right.setText("");
        rightLayout.setBackgroundDrawable(null);
    } else {
        right.setText(getItem(position));
        rightLayout.setBackgroundDrawable(bubblesResponse);
        rightParams.weight = 0.8f;
        linearRight.setLayoutParams(rightParams);           
        leftParams.weight = 0.2f;
        linearLeft.setLayoutParams(leftParams);
        left.setText("");
        leftLayout.setBackgroundDrawable(null);
    }
    return view;

我从这个设置中得到的结果如下(注意右边气泡前面的空白区域:

enter image description here

您可以看到右侧的气泡没有根据文本大小包裹宽度。我明白为什么会发生这种情况 - 我将 0.8 的权重分配给当前的聊天消息气泡(可能从左到右),将 0.2 的权重分配给其余的。当当前消息来自左侧气泡时,它工作正常,因为它首先使用 wrap_content 绘制为 0.8 权重。但是当右边的气泡是当前消息时,左边的气泡首先被绘制并且宽度固定为 0.2,因此无论 wrap_content 是什么,右边的气泡总是 0.8。我怎样才能解决这个问题?我想要的只是根据文本宽度获取气泡并左对齐或右对齐。如果您能建议一种更好的方法来正确执行此操作,我可以完全放弃我目前的方法。

最佳答案

当我第一次发布这个问题时,我才刚刚开始 Android 编程。问题是我的布局定义和代码过于复杂。现在我简化了我的布局层次结构,我已经实现了我想要的并且没有使用任何布局权重并且有很多简单的代码/配置。我在这里发布我更新的代码 fragment 。

我现在的主要布局:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent">

    <ListView android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:cacheColorHint="#00000000"
        android:listSelector="@android:color/transparent"
        android:divider="@null"/>

    <LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:gravity="bottom" style="@android:style/ButtonBar">
        <Button android:id="@+id/stt_button"
            android:layout_width="45dp"
            android:layout_height="50dp"
            android:background="@drawable/microphone"/>   

        <EditText android:id="@+id/chat_msg"
            android:inputType="text" 
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_weight="1" />

        <Button android:id="@+id/send_button"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:text="@string/send_button" />
    </LinearLayout>
</LinearLayout>

列表项布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:weightSum="1.0"
    android:layout_weight="1" android:layout_height="wrap_content">

    <TextView android:id="@+id/lefttext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dip" 
        android:layout_marginRight="10dip" 
        android:layout_marginTop="10dip"
        android:layout_marginBottom="5dip"
        android:layout_alignParentLeft="true"
        android:maxWidth="250dip"/>

    <TextView 
        android:id="@+id/righttext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip" 
        android:layout_marginRight="10dip" 
        android:layout_marginTop="10dip"
        android:layout_marginBottom="5dip"
        android:layout_alignParentRight="true"
        android:maxWidth="250dip"/>
</RelativeLayout>

这是我的自定义数组适配器的 getView 方法中的代码:

    View view = convertView;
    if(view == null){
         view = mInflater.inflate(R.layout.list_item, null);
    }

    Resources res = getContext().getResources();
    Drawable bubblesChat = res.getDrawable(R.drawable.bubbles_chat);
    Drawable bubblesResponse = res.getDrawable(R.drawable.bubbles_response);
    TextView left = (TextView) view.findViewById(R.id.lefttext);
    TextView right = (TextView) view.findViewById(R.id.righttext);

    String txt = super.getItem(position);
    if(txt.startsWith("s:")) {
        left.setText(getItem(position));
        left.setBackgroundDrawable(bubblesChat);
        right.setText("");
        right.setBackgroundDrawable(null);
    } else {
        right.setText(getItem(position));
        right.setBackgroundDrawable(bubblesResponse);
        left.setText("");
        left.setBackgroundDrawable(null);
    }
    return view;

关于android - 备用聊天气泡宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9101835/

相关文章:

java - 二进制 XML 文件导致的展开布局错误

java - 将 Activity 元素的排列设置为从左到右(默认)

android - Facebook 广告集成到 Android 应用中

android - 在 scrollView android 上实现缩放

android - 在 Android Tabview 中捕获选项卡单击事件

android - 根据 Android 使用的 Fragment 更改 Activity 主题

java - 在android中导入字体时出现空指针异常

java - Android Bitmap.class 中 native DecodeByteArray 的问题

android - 从 Android Assist API 检索所有文本内容

java - 在类中使用 findViewById 时出现问题