本文介绍了ListView控件的RowLayout在自己的codeD聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做

我试图创建一个具有不同势行的聊天。对于每一行我做了一个自己的布局文件,但问题是,一排的layoutfile不到风度适合屏幕。

我需要什么,它适合像它应该排布局改变。你会发现我想要的code,也是一个PRINTSCREEN。

code


ListAdapter
    公共类ChatListAdapter延伸BaseAdapter {

 私人的ArrayList<串GT; ContentList;
            私人的ArrayList<整数GT; ChatUserList;            私有静态LayoutInflater吹气= NULL;            公共ChatListAdapter(活动活动,ArrayList的<串GT; _ContentList,
                                   ArrayList的<整数GT; _ChatUserList){
                        ContentList = _ContentList;
                        ChatUserList = _ChatUserList;
                        吹气=(LayoutInflater)活动
                                               .getSystemService(Context.LAYOUT_INFLATER_SERVICE);            }            公众诠释的getCount(){
                        返回ContentList.size();
            }            公共对象的getItem(INT位置){
                        返回的位置;
            }            众长getItemId(INT位置){
                        返回的位置;
            }            公共查看getView(INT位置,查看convertView,父母的ViewGroup){
                        查看VI = convertView;
                        TextView的tv_text;
                        TextView的tv_date;                        SimpleDateFormat的SDF =新的SimpleDateFormat(HH:MM);                        如果(ChatUserList.get(位置)== 1){
                                   //我RECIVER - >消息被左对齐
                                   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);
                        }其他{
                                   //我SENDER - >消息是正确对齐
                                   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(新的Date()));
                        tv_text.setText(ContentList.get(位置));
                        tv_text.setMaxWidth(((Chat.display.getWidth()/ 4)* 3));                        返回VI;
            }

的RowLayout XML左(蓝色气泡)

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:背景=@机器人:彩色/透明>    < RelativeLayout的
        机器人:ID =@ + ID / ly_rf_row_r
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_marginBottom =2.5dp
        机器人:layout_marginTop =2.5dp
        机器人:背景=@绘制/ shape_rightalign
        机器人:paddingBottom会=2.5dp
        机器人:paddingTop =2.5dp>        <的TextView
            机器人:ID =@ + ID / tv_chat_rightalign
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真
            机器人:重力=右
            机器人:paddingBottom会=2DP
            机器人:paddingLeft =5DP
            机器人:paddingRight =5DP
            机器人:paddingTop =2DP
            机器人:文字颜色=@机器人:彩色/黑白/>        <的TextView
            机器人:ID =@ + ID / tv_chat_rightalign_date
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentBottom =真
            机器人:layout_marginBottom = - 3DP
            机器人:layout_marginLeft = - 3DP
            机器人:layout_toLeftOf =@ ID / tv_chat_rightalign
            机器人:paddingLeft =4DP
            机器人:paddingRight =4DP
            机器人:文字颜色=@机器人:彩色/黑白
            机器人:TEXTSIZE =8.5dp/>
    < / RelativeLayout的>< / RelativeLayout的>

的RowLayout XML右(红色气泡)

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:背景=@机器人:彩色/透明>    < RelativeLayout的
        机器人:ID =@ + ID / ly_rf_row_l
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_marginBottom =2.5dp
        机器人:layout_marginTop =2.5dp
        机器人:背景=@绘制/ shape_leftalign
        机器人:paddingBottom会=2.5dp
        机器人:paddingTop =2.5dp>        <的TextView
            机器人:ID =@ + ID / tv_chat_leftalign
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentLeft =真
            机器人:重力=左
            机器人:paddingBottom会=2DP
            机器人:paddingLeft =5DP
            机器人:paddingRight =5DP
            机器人:paddingTop =2DP
            机器人:文字颜色=@机器人:彩色/黑白/>        <的TextView
            机器人:ID =@ + ID / tv_chat_leftalign_date
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentBottom =真
            机器人:layout_marginBottom = - 3DP
            机器人:layout_marginLeft = - 3DP
            机器人:layout_toRightOf =@ ID / tv_chat_leftalign
            机器人:paddingLeft =4DP
            机器人:paddingRight =4DP
            机器人:文字颜色=@机器人:彩色/黑白
            机器人:TEXTSIZE =8.5dp/>
    < / RelativeLayout的>< / RelativeLayout的>


解决方案

为XML添加的android:layout_width =FILL_PARENT的android: layout_height =WRAP_CONTENT像以下

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:背景=@机器人:彩色/透明
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =FILL_PARENT>

等等...

What I'm trying to do

I'm trying to create a chat which has to diffrent row's. For every row I made a own layout file, but the problem is that the layoutfile of one row dosn't fit the screen.

Question

What do I need to change in the row layout that it fits like it should. You'll find the code and also a printscreen of what I'm trying.

Code


ListAdapter public class ChatListAdapter extends 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 (Blue bubbles)

 <?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 right (red bubbles)

 <?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>
解决方案

for both XML add android:layout_width="fill_parent" and android:layout_height="wrap_content" like followings

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

and so on...

这篇关于ListView控件的RowLayout在自己的codeD聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 22:45