我正在尝试创建聊天视图(已发送文本,已接收文本)。背景可绘制(框)与一侧正确对齐。当我滚动时,它会扩展框以适应整个宽度。不知道为什么会这样。有任何想法吗? The problem i'm talking about How i actually want it(this is before scrolling)
我的自定义适配器类
public class DisplayMessageAdapter extends ArrayAdapter<Message> {
Context context;
int sentResource, rcvdResource;
ArrayList<Message> messages = null;
public DisplayMessageAdapter(Context context, int sentResource, int rcvdResource, ArrayList<Message> messages) {
super(context, sentResource, rcvdResource, messages);
this.context = context;
this.sentResource = sentResource;
this.rcvdResource = rcvdResource;
this.messages = messages;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Message message = messages.get(position);
Holder holder = new Holder();
if(row == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder = new Holder();
row = inflater.inflate(R.layout.message_layout, parent, false);
holder.content = (TextView) row.findViewById(R.id.message_content);
row.setTag(holder);
}
else
{
holder = (Holder) row.getTag();
}
if(message.sent != null)
{
RelativeLayout.LayoutParams params = (LayoutParams) holder.content.getLayoutParams();
params.setMargins(30, params.topMargin, params.rightMargin, params.bottomMargin);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.content.setLayoutParams(params);
holder.content.setBackgroundResource(R.drawable.sent);
holder.content.setText(message.sent);
}
else
{
RelativeLayout.LayoutParams params = (LayoutParams) holder.content.getLayoutParams();
params.setMargins(params.leftMargin, params.topMargin, 30, params.bottomMargin);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.content.setLayoutParams(params);
holder.content.setBackgroundResource(R.drawable.rcvd);
holder.content.setText(message.received);
}
return row;
}
我的布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/message_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:textSize="15sp"
/>
</RelativeLayout>
我的Drawables - rcvd / send
<shape android:visible="false" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp"/>
<stroke android:color="#439CC8" android:width="2dp"/>
</shape>
<shape android:visible="false" xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp"/>
<stroke android:color="#6D4E4E" android:width="2dp"/>
</shape>