我正在尝试实现一个可以出现在左侧或右侧的聊天气泡。我阅读了很多已经在这里发布的答案,但我没有设法让其中任何一个为我工作,也许我错过了一些东西。
如果我手动从左到右更改 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;
}
}