【问题标题】:ListView Rowlayout in a own coded ChatListView Rowlayout 在自己的编码聊天中
【发布时间】:2012-06-27 13:04:24
【问题描述】:

我要做什么

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

问题

我需要在行布局中进行哪些更改以使其符合预期。您会找到代码以及我正在尝试的打印屏幕。

代码


列表适配器 公共类 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 左侧(蓝色气泡)

 <?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>

【问题讨论】:

    标签: android android-layout chat


    【解决方案1】:

    对于 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" >
    

    等等……

    【讨论】:

    • 不清楚什么代表红色聊天和蓝色聊天。我通过查看您的代码来提供此答案。
    • 看看打印屏幕,你会看到一个蓝色和红色的消息气泡;)这就是我的意思
    • 我确信你在这两种布局中都有问题,实际上形成了一个距离,如果不实际检查它就不可能检测到确切的错误。所以我的建议是在所有项目中彻底检查这两个布局,主要是 layout_width/height 部分,alignParentRight/Left 部分。希望你能解决你的问题。一切顺利。
    • 我重新编写了整个代码.. 不管它现在如何工作.. 但绿色的勾是给你的
    猜你喜欢
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    相关资源
    最近更新 更多