2014-11-03 125 views

回答

1

我hava还搜索了一个Xamarin示例项目,显示如何制作一个聊天应用程序,但没有找到一个。也许你可以从Android的Java样本项目得到帮助像AndroidChatBubbles

这是我自己的项目的一些布局

活动的XML视图:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<!-- Displays the text chat --> 
    <ListView 
     android:id="@+id/forms_centralfragments_chat_chat_listView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_above="@+id/forms_centralfragments_chat_chat_editLayout" 
     android:background="@color/list_line_seperate" 
     android:clipToPadding="false" 
     android:listSelector="#00000000" 
     android:divider="@null" 
     android:paddingBottom="@dimen/controlcenter_layout_height" /> 
    <LinearLayout 
     android:id="@+id/forms_centralfragments_chat_chat_editLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="10dp" 
     android:layout_alignParentBottom="true" 
     android:stackFromBottom="true" 
     android:transcriptMode="alwaysScroll" 
     android:orientation="horizontal"> 
     <EditText 
      android:id="@+id/forms_centralfragments_chat_chat_editText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="textMultiLine" 
      android:layout_marginRight="10dp" 
      android:paddingBottom="10dp" 
      android:paddingTop="12dp" 
      android:layout_weight="1" 
      android:background="#FFFFFF" /> 
     <Button 
      android:id="@+id/forms_centralfragments_chat_chat_sendButton" 
      android:layout_width="70dp" 
      android:layout_height="40dp" 
      android:background="@drawable/button_style" 
      android:text="Senden" 
      android:textColor="#FFFFFF" /> 
    </LinearLayout> 
</RelativeLayout> 

泡沫的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
<TextView 
     android:id="@+id/list_bubble_userName" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_margin="10dp" 
     android:maxLines="1" 
     android:singleLine="true" 
     android:textColor="@color/text_color_black" /> 
    <TextView 
     android:id="@+id/list_bubble_message" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:maxWidth="230dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:background="@drawable/bubble_green" 
     android:paddingLeft="10dp" 
     android:textColor="@color/text_color_black" /> 
</LinearLayout> 

和适配器:

public class BubbleAdapter : ArrayAdapter<BubbleEntity> 
    { 

     /// <summary> 
     /// The context of the activity 
     /// </summary> 
     private Activity _context; 

     /// <summary> 
     /// The list that holds the bubble strings 
     /// </summary> 
     private List<BubbleEntity> _bubbleList; 

     /// <summary> 
     /// Creates a new Instance of the <see cref="FloatStringAdapter"/> - Class 
     /// </summary> 
     /// <param name="context"></param> 
     /// <param name="floatStringList"></param> 
     public BubbleAdapter(Activity context, List<BubbleEntity> bubbleList) 
      : base(context, Resource.Layout.list_bubble, bubbleList) 
     { 
       this._context = context; 
       this._bubbleList = bubbleList; 
     } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
      { 

       // Get our object for this position 
       var item = this._bubbleList[position]; 
       var view = (convertView ?? 
         this._context.LayoutInflater.Inflate(
         Resource.Layout.list_bubble, 
         parent, 
         false)) as LinearLayout; 

       TextView username = view.FindViewById<TextView>(Resource.Id.list_bubble_userName); 
       TextView message = view.FindViewById<TextView>(Resource.Id.list_bubble_message); 
       username.TextFormatted = Html.FromHtml(item.UserNameText); 
       message.Text = item.Text; 
       if(item.IsTheDeviceUser == false) 
       { 
        view.SetGravity(GravityFlags.Left); 
        message.SetBackgroundResource(Resource.Drawable.bubble_other);  
       } 
       else 
       { 
        view.SetGravity(GravityFlags.Right); 
        message.SetBackgroundResource(Resource.Drawable.bubble_user); 
       } 

       return view; 
      } 
} 

请注意,BubbleEntity(传输消息数据的实体)包含一个bool字段IsDeviceUser,用于指示消息来自哪里(设备用户或其他聊天),因此它会更改图像的对齐方式和drawable的泡沫。不要忘记,你应该为气泡使用9个补丁图像,否则你将会遇到缩放问题。