4

我正在尝试实现一个可以出现在左侧或右侧的聊天气泡。我阅读了很多已经在这里发布的答案,但我没有设法让其中任何一个为我工作,也许我错过了一些东西。

如果我手动从左到右更改 row_item xml 中 linearLayout 的 layout_gravity 然后查看图形布局,我可以看到它按我想要的方式工作。问题是我无法控制这个属性。据我了解,问题在于存在的 LayoutParams 属于 listview.LayoutParams 的事实,如果我尝试将 LayoutParams 获取到 listview.LayoutParams 对象,它就可以工作。但这不是我想要达到的目标。

这是代码:

活动 xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MergeRootFrame" >

<ListView
    android:id="@+id/listViewMessages"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="0.98" >

</ListView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.02"
    android:background="#D3D3D3"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/imageButtonSend"
        android:ems="10"
        android:hint="@string/textFieldMessage"
        android:textSize="14sp"
        android:visibility="visible" >

        <requestFocus />
    </EditText>

    <ImageButton
        android:id="@+id/imageButtonSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="14dp"
        android:background="?android:attr/selectableItemBackground"
        android:contentDescription="@string/imageButtonSendNow_contentDescription"
        android:src="@drawable/ic_action_send_now" 
        android:onClick="onClick" />

</RelativeLayout>

</LinearLayout>

row_item xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/singleMessageContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal"
android:padding="6dp" >

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_weight="0.85"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:gravity="center_vertical"
        android:text="Message"
        android:textSize="16sp"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/TextViewTime"
        android:layout_width="wrap_content"
        android:layout_height="14dp"
        android:layout_weight="0.1"
        android:layout_gravity="bottom"
        android:layout_marginLeft="22dp"
        android:gravity="center_vertical"
        android:text="Time"
        android:textSize="12sp"
        tools:ignore="HardcodedText" />

    <ImageView
        android:id="@+id/imageViewSuccess"
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:layout_weight="0.05"
        android:layout_gravity="bottom"
        android:contentDescription="successIcon"
        android:src="@drawable/ic_action_accept"
        tools:ignore="HardcodedText" />

  </LinearLayout>

适配器代码:

public class ConversationAdapter extends ArrayAdapter<Message> {

private final Context context;

static class ViewHolder {
    public TextView message;
    public TextView time;
    public ImageView success;
    public LinearLayout singleMessageContainer;

}

public ConversationAdapter(Context context, ArrayList<Message> values) {
    super(context, R.layout.conversation_list_item, values);
    this.context = context;

}

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
      View rowView = convertView;
        // reuse views
        if (rowView == null) {
          LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          rowView = inflater.inflate(R.layout.conversation_list_item, parent,false);
          // configure view holder
          ViewHolder viewHolder = new ViewHolder();
          viewHolder.message = (TextView) rowView.findViewById(R.id.textViewMessage);
          viewHolder.time = (TextView) rowView.findViewById(R.id.TextViewTime);
          viewHolder.success = (ImageView) rowView.findViewById(R.id.imageViewSuccess);
          viewHolder.singleMessageContainer = (LinearLayout)     rowView.findViewById(R.id.singleMessageContainer);

          rowView.setTag(viewHolder);
        }

        // fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.message.setText(getItem(position).getMessage());
        holder.time.setText(getItem(position).getUniversalTimeString());
        holder.success.setImageResource(R.drawable.ic_action_accept);

        boolean isMine = (getItem(position).getSender() == 0);

        holder.singleMessageContainer.setBackgroundResource(
                isMine ? R.drawable.bubble_yellow : R.drawable.bubble_green);

        // This part doesn't work
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) 
                holder.singleMessageContainer.getLayoutParams();
        params.gravity=(isMine ? Gravity.LEFT : Gravity.RIGHT);

        holder.singleMessageContainer.setLayoutParams(params);

        return rowView; 
  }

}
4

1 回答 1

2

解决方案

我继续阅读有关重力和 layout_gravity 的内容,并遇到了帮助我解决此问题的更详细信息。在此处对问题的几个答案中,有一个使用 warpper 布局的解决方案,但没有写出需要注意的重要事项。

首先要知道的是,您将线性布局设置为水平,您将无法将重力属性设置为仅顶部或底部的左侧或右侧,因此外部容器必须是垂直的,而内部扭曲器必须是水平的以显示消息和其他数据一个接一个地排成一行。

然后您可以使用外部容器的 setGravity(Gravity.LEFT) 或 setGravity(Gravity.RIGHT) 方法将内部经纱器正确对齐到左侧或右侧。您还需要注意 fill_parent 和 wrap_content 的属性

这里更新的代码:row_item xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/singleMessageContainer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
android:padding="6dp" >

<LinearLayout
     android:id="@+id/warpper"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="horizontal" >

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="2dp"
        android:text="Message"
        android:textSize="14sp"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/TextViewTime"
        android:layout_width="wrap_content"
        android:layout_height="14dp"
        android:layout_gravity="bottom"
        android:text="Time"
        android:textSize="12sp"
        tools:ignore="HardcodedText" />

    <ImageView
        android:id="@+id/imageViewSuccess"
        android:layout_width="wrap_content"
        android:layout_height="12dp"
        android:layout_gravity="bottom"
        android:contentDescription="successIcon"
        android:src="@drawable/ic_action_accept"
        tools:ignore="HardcodedText" />

    </LinearLayout>

</LinearLayout>

和更新的适配器代码:

public class ConversationAdapter extends ArrayAdapter<Message> {

private final Context context;

static class ViewHolder {
    public TextView message;
    public TextView time;
    public ImageView success;
    public LinearLayout singleMessageContainer;
    // Added
    public LinearLayout warpper;

}

public ConversationAdapter(Context context, ArrayList<Message> values) {
    super(context, R.layout.conversation_list_item, values);
    this.context = context;

}

  @Override
public View getView(int position, View convertView, ViewGroup parent) {
      View rowView = convertView;
        // reuse views
        if (rowView == null) {
          LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          rowView = inflater.inflate(R.layout.conversation_list_item, parent,false);
          // configure view holder
          ViewHolder viewHolder = new ViewHolder();
          viewHolder.message = (TextView) rowView.findViewById(R.id.textViewMessage);
          viewHolder.time = (TextView) rowView.findViewById(R.id.TextViewTime);
          viewHolder.success = (ImageView) rowView.findViewById(R.id.imageViewSuccess);
          viewHolder.singleMessageContainer = (LinearLayout) rowView.findViewById(R.id.singleMessageContainer);
          // Added
          viewHolder.warpper = (LinearLayout) rowView.findViewById(R.id.warpper);

          rowView.setTag(viewHolder);
        }

        // fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();

        holder.message.setText(getItem(position).getMessage());
        holder.time.setText(getItem(position).getUniversalTimeString());
        holder.success.setImageResource(R.drawable.ic_action_accept);

        boolean isMine = (getItem(position).getSender() == 0);

        // Changed to warpper
        holder.warpper.setBackgroundResource(
                isMine ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        // Added
        holder.singleMessageContainer.setGravity(isMine ? Gravity.LEFT : Gravity.RIGHT);


        return rowView; 
  }

}

它应该工作。唯一的问题是,您可能会在 xml 中看到有关线性布局之一无用的警告,但如果没有它,它将无法工作。

希望它会帮助你。

于 2014-08-18T13:41:28.037 回答