ListView Rowlayout在自己的编码聊天中

时间:2012-06-27 13:04:25

标签: android android-layout chat

我正在尝试做什么

我正在尝试创建一个不同行的聊天。对于每一行我都创建了一个自己的布局文件,但问题是一行的布局文件不适合屏幕。

问题

我需要更改它所适合的行布局。你会找到我正在尝试的代码和打印屏幕。

enter image description here

代码


ListAdapter     公共类ChatListAdapter扩展了BaseAdapter {

            private ArrayList<String> ContentList;
            private ArrayList<Integer> ChatUserList;

            private static LayoutInflater inflater = null;

            public ChatListAdapter(Activity activity, ArrayList<String> _ContentList,
                                   ArrayList<Integer> _ChatUserList) {
                        ContentList = _ContentList;
                        ChatUserList = _ChatUserList;
                        inflater = (LayoutInflater) activity
                                               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            }

            public int getCount() {
                        return ContentList.size();
            }

            public Object getItem(int position) {
                        return position;
            }

            public long getItemId(int position) {
                        return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                        View vi = convertView;
                        TextView tv_text;
                        TextView tv_date;

                        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");

                        if (ChatUserList.get(position) == 1) {
                                   // I'm RECIVER -> Message is left align
                                   vi = inflater.inflate(R.layout.rowlayout_leftalign, null);
                                   tv_text = (TextView) vi.findViewById(R.id.tv_chat_leftalign);
                                   tv_date = (TextView) vi.findViewById(R.id.tv_chat_leftalign_date);
                        } else {
                                   // I'm SENDER -> Message is right align
                                   vi = inflater.inflate(R.layout.rowlayout_rightalign, null);
                                   tv_text = (TextView) vi.findViewById(R.id.tv_chat_rightalign);
                                   tv_date = (TextView) vi.findViewById(R.id.tv_chat_rightalign_date);
                        }

                        tv_date.setText(sdf.format(new Date()));
                        tv_text.setText(ContentList.get(position));
                        tv_text.setMaxWidth(((Chat.display.getWidth() / 4) * 3));

                        return vi;
            }

RowLayout XML Left(蓝色气泡)

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:id="@+id/ly_rf_row_r"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="2.5dp"
        android:layout_marginTop="2.5dp"
        android:background="@drawable/shape_rightalign"
        android:paddingBottom="2.5dp"
        android:paddingTop="2.5dp" >

        <TextView
            android:id="@+id/tv_chat_rightalign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:gravity="right"
            android:paddingBottom="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="2dp"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/tv_chat_rightalign_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="-3dp"
            android:layout_marginLeft="-3dp"
            android:layout_toLeftOf="@id/tv_chat_rightalign"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:textColor="@android:color/black"
            android:textSize="8.5dp" />
    </RelativeLayout>

</RelativeLayout>

RowLayout XML右(红色气泡)

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/transparent" >

    <RelativeLayout
        android:id="@+id/ly_rf_row_l"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="2.5dp"
        android:layout_marginTop="2.5dp"
        android:background="@drawable/shape_leftalign"
        android:paddingBottom="2.5dp"
        android:paddingTop="2.5dp" >

        <TextView
            android:id="@+id/tv_chat_leftalign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:gravity="left"
            android:paddingBottom="2dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="2dp"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/tv_chat_leftalign_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="-3dp"
            android:layout_marginLeft="-3dp"
            android:layout_toRightOf="@id/tv_chat_leftalign"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:textColor="@android:color/black"
            android:textSize="8.5dp" />
    </RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

对于XML添加android:layout_width="fill_parent"android:layout_height="wrap_content",如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="@android:color/transparent" 
            android:layout_height="wrap_content"
            android:layout_width="fill_parent" >

依旧......