自定义ListView项目将列表视图向左移动?

时间:2013-11-25 15:26:16

标签: android android-layout android-listview textview imageview

我的自定义列表视图存在某种独特的问题,我无法追踪问题所在。      自定义列表视图用于我正在创建的聊天应用程序,其中我使用用户输入的消息动态添加文本视图。添加了每个文本视图后,列表视图将移动到屏幕的左侧,文本视图enter image description here在左侧被切割,我搜索了很多可能是原因但不能找到任何人,有人可以帮我解决这个问题吗?      图像显示每个条目在右侧增加的间隙。 这是xml代码

<RelativeLayout
    android:id="@+id/chatheader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/headershowfriends" >

    <TextView
        android:id="@+id/namechatfriend"
        style="@style/Text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="demo" />

    <ImageView
        android:id="@+id/frdstatus"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/online" />
</RelativeLayout>

<ListView
    android:id="@+id/myList"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_above="@+id/msgboxll"
    android:layout_below="@+id/chatheader"
    android:layout_marginBottom="50dp"
    android:layout_marginLeft="10dp"
    android:dividerHeight="0dp"
    android:divider="@null"
    android:fitsSystemWindows="true" />

<LinearLayout
    android:id="@+id/msgboxll"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearchat"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="@drawable/headershowfriends"
        android:orientation="horizontal"
        android:weightSum="1" >

        <Button
            android:id="@+id/smiles"
            android:layout_width="20dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="1dp"
            android:layout_weight="0.1"
            android:background="@drawable/chat_smile" />

        <EditText
            android:id="@+id/messageBx"
            style="@drawable/edittext"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="2dp"
            android:layout_weight="0.80" >
        </EditText>

        <Button
            android:id="@+id/sendBtn"
            style="@style/button_text"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="3dp"
            android:layout_weight="0.1"
            android:text="@string/send" />
    </LinearLayout>

    <com.pack.coderzheaven.SlidingPanel
        android:id="@+id/popup_smilywindow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/blackbckgnd"
        android:gravity="left"
        android:orientation="vertical"
        android:visibility="invisible" >

        <GridView
            android:id="@+id/gridViewsmily"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:columnWidth="30dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="5"
            android:verticalSpacing="10dp" >
        </GridView>
    </com.pack.coderzheaven.SlidingPanel>
</LinearLayout>

`

这是我的适配器 `

public class ChatMessageAdapter extends  ArrayAdapter<Chat>
{
        List<Chat>listmsg=new ArrayList<Chat>(); 
        Activity context;
        Bitmap bitmapfriend,bitmapuser;
        public ChatMessageAdapter(Activity context,List<Chat> listmsg, Bitmap friendimage, Bitmap userimage) 
        {
            super(context, R.layout.list_row_layout_even, listmsg);
            this.listmsg=listmsg;
            this.context=context;
            this.bitmapfriend=friendimage;
            this.bitmapuser=userimage;
        }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        try 
        {
            try 
            {
        LayoutInflater inflater = context.getLayoutInflater();

        String type=listmsg.get(position).getType();

        if(type.contentEquals("even"))
        {
            convertView = inflater.inflate(R.layout.list_row_layout_even, parent, false);
            TextView textLabel = (TextView) convertView.findViewById(R.id.text);
            textLabel.setText(listmsg.get(position).getMessageSpannable());
            ImageView image=(ImageView) convertView.findViewById(R.id.rightProfilePic);
            if (bitmapuser==null) 
            {

            }
            else
            {
            image.setImageBitmap(bitmapuser);
            }   
        }
        else if(type.contentEquals("odd"))
        {
            convertView = inflater.inflate(R.layout.list_row_layout_odd, parent, false);
            TextView textLabel = (TextView) convertView.findViewById(R.id.text);
            textLabel.setText(listmsg.get(position).getMessageSpannable());
            ImageView image=(ImageView) convertView.findViewById(R.id.leftProfilePic);
            image.setImageBitmap(bitmapfriend);
        }           
            }
            catch (Exception e) 
            {
                throw new IOException("Error connecting");
            }
        } 
        catch (Exception e) 
        {

        }       
        return (convertView);
    }

} `

1 个答案:

答案 0 :(得分:0)

首先,感谢帮助我,我的问题在我创建一个新的xml后自动解决了。听起来很奇怪,但它确实发生了。