0
我發現BubbleCell一個Xamarin應用程序的ios,但我需要一個android應用程序相同的佈局。我在Google上找不到任何好的教程或示例。任何人都可以請給我一個教程或鏈接的例子。Xamarin Android的ChatBubble佈局
我發現BubbleCell一個Xamarin應用程序的ios,但我需要一個android應用程序相同的佈局。我在Google上找不到任何好的教程或示例。任何人都可以請給我一個教程或鏈接的例子。Xamarin Android的ChatBubble佈局
我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個補丁圖像,否則你將會遇到縮放問題。