最近在看Xamarin使用C#來撰寫Android App .

紀錄一下,順便給之後有需要的人可以有所參考 :)

今天要來聊的是關於Toast 這東西,這在以前Android 上面我是很常使用

拿來log 做debug 或是做一些給User 的簡單提示都是非常方便的.

Toast樣貌:

首先規劃兩個按鈕一個點擊後就是顯示傳統的Toast ,

另外一個我們來測試有點變化圖片+文字的Toast 首先看一下主畫面 Main.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnToast1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="測試傳統吐司" />
    <Button
        android:id="@+id/btnToast2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="測試文情並茂吐司" />
</LinearLayout>

其實也沒啥好解釋的兩顆按鈕,第一顆 bntToast1,按下去後就會顯示傳統的Toast

第二顆按鈕按下去後就是出現有圖文加在一起的Toast

來看Code.

Activity1.cs:

using Android.App;
using Android.Widget;
using Android.OS;
 
namespace XamarinToastSamle
{
    [Activity(Label = "測試吐司Sample", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            SetContentView(Resource.Layout.Main);
 
 
            //取得btnToast1
            var btnToast1 = FindViewById<Button>(Resource.Id.btnToast1);
            //設定其Click 事件
            btnToast1.Click += delegate
                {
                    Toast.MakeText(this, "Hello World.Xamarin", ToastLength.Short).Show();
                };
 
        }
    }
}
 

傳統Toast非常的簡單,只要

Toast.MakeText(預被掛載的Context通常是Activity 或是Application , 顯示文字 , 顯示時間的長短)

結果:

很簡單吧,再來我們要如何顯示圖文並茂的Toast

首先我們要先建立一個Layout來幫忙,首先我們在 Resources 的Layout中加入一個TaostPopItem.axml

首先看一下畫面:

看一下它的原始XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

他預設為 LinearLayout 但是因為排版關係,我將原始的LinearLayout 改成 我需要的 RelativeLayout

關於這些Android Layout 設計哲學 可以參考這裡 : http://developer.android.com/guide/topics/ui/declaring-layout.html

我就將不贅述再來我們透過左邊工具箱,拉入ImageView 還有TextView

在拉TextView的時候有一個小技巧,如果拉在ImageView旁邊的藍線位置他的預設 layout_toRightOf 的property 會是該物件

之後我們調整一下一些Property 修正一下使得結果如下

ToastPopItem.axml:

<?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">
    <ImageView
        android:src="@android:drawable/ic_menu_gallery"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imageView1" />
    <TextView
        android:text="Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imageView1"
        android:id="@+id/textView1"
        android:paddingTop="15dp" />
</RelativeLayout>

畫面:

因為我們需要用到一張圖片,所以我複製一張InfoIcon.png 的圖片至 Resource > Drawable > InfoIcon.png 之下

之後給程式呼叫用再來回到主Activity :

加上btnToast2 的Click 使得Code 為:

using Android.App;
using Android.Widget;
using Android.OS;
 
namespace XamarinToastSamle
{
    [Activity(Label = "測試吐司Sample", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
 
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
 
            SetContentView(Resource.Layout.Main);
 
 
            //取得btnToast1
            var btnToast1 = FindViewById<Button>(Resource.Id.btnToast1);
            //設定其Click 事件
            btnToast1.Click += delegate
            {
                Toast.MakeText(this, "Hello World.Xamarin", ToastLength.Short).Show();
            };
 
            //取得btnToast2
            var btnToast2 = FindViewById<Button>(Resource.Id.btnToast2);
            //設定其Click事件
            btnToast2.Click += delegate
            {
                var view = LayoutInflater.Inflate(Resource.Layout.ToastPopItem, null);
 
                var imgMain = view.FindViewById<ImageView>(Resource.Id.imageView1);
                var txt = view.FindViewById<TextView>(Resource.Id.textView1);
                
                //載入Drawable 中的 InfoIcon.png
                imgMain.SetImageResource(Resource.Drawable.InfoIcon);
                txt.Text = "圖文並茂的吐司";
                var toast = new Toast(this);
                //設定該View
                toast.View = view;
                toast.Show();
 
            };
 
        }
    }
}
 

結果:

[Xamarin] 製作吐司(Toast)以及圖文並茂的Toast (转帖)的更多相关文章

  1. [Xamarin] 製作Options Menu、Intent 呼叫網址和Market (转帖)

    Android的設計如果沒意外的話通常有三棵按鈕,BACK,HOME,OPTION (圖片來源:http://developer.android.com/design/index.html) 在OPT ...

  2. [Xamarin] 用Service 來製作一個Notification的時鐘 (转帖)

    這篇利用來製作一個會出現在Notification的時鐘,來敘述一下 Service,在你製作的App被關閉時,可以透過Service繼續運行你想處理的部分,當然Service 也有其生命周期 接下來 ...

  3. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  4. [Xamarin] 透過 intent-filter 來接管 http ,製作偽瀏覽器 (转帖)

    使用Android 的朋友一定對這畫面不陌生在開啟網址的時候,或是Youtube連結的時候,因為Android 發現,你手機安裝的App有哪些可以支援這些東西的瀏覽 所以,就可以使用甚麼東西來進行開啟 ...

  5. CSS製作動畫效果(Transition、Animation、Transform)

    CSS 2D Transforms https://www.w3schools.com/css/css3_2dtransforms.asp CSS 3D Transforms https://www. ...

  6. yii框架製作簡易RBAC權限管理

    控制器源碼 <?php namespace app\controllers; use yii; use yii\web\Controller; class PowerController ext ...

  7. [Xamarin] 啟動拍照並且儲存 (转帖)

    拍照對手機來說是很常用到的功能,許多App都基於在拍照上面,這篇文章主要大部分是在翻譯官方文件 (http://docs.xamarin.com/recipes/android/other_ux/ca ...

  8. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet (转帖)

    JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet 功能:這支程式的主要功能是將 javafx 與 swi ...

  9. JavaScript從剪切板中獲取圖片並在光標處插入

    edit_content_text.addEventListener('paste', function (ev) {    var clipboardData, items, item;    co ...

随机推荐

  1. asp.net core 1.1 mysqlsugarCore mysql.data 要 7.0.5.0

    Message=Could not load file or assembly 'MySql.Data, Version=7.0.5.0, Culture=neutral, PublicKeyToke ...

  2. mvc html.PartialView()传参

    方式一,viewDatapublic static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, ...

  3. Hadoop中Writable类之四

    1.定制Writable类型 Hadoop中有一套Writable实现,例如:IntWritable.Text等,但是,有时候可能并不能满足自己的需求,这个时候,就需要自己定制Writable类型. ...

  4. [转]How do I run msbuild from the command line using Windows SDK 7.1?

    本文转自:http://stackoverflow.com/questions/6319274/how-do-i-run-msbuild-from-the-command-line-using-win ...

  5. EBS通过SQL查找所有的定时请求

    --查找所有定时请求. --也可以登录系统,在系统管理员下查找特定请求,状态设置为Scheduled进行查询SELECT DISTINCT USER_CONCURRENT_PROGRAM_NAME,B ...

  6. Mirth Connect的简单使用

    第一步: 切换到Channels界面,右键点击New Channel 第二步 : 上面是设置一些通道信息. 其中summary(概要) 界面主要包含 通道名称,数据类型,依赖,通道初始状态,附件(是否 ...

  7. Python学习-9.Python函数定义

    先定义一个最基本的函数作为例子: def Print(msg): print(msg) 函数名为Print,参数有一个,为msg,函数体调用print系统函数,输出msg. 接下来就是可变参数,这个特 ...

  8. [LeetCode 题解]: Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  9. js如何给当前日期+1?

    一天=24小时=1440分钟=86400秒 所以给当前日期加一天的步骤为: 1.获取当前日期: 2.利用86400秒给其进行加一天操作: 3.类似加一天,两天,一月,一年等,过程如此. 代码如下(以j ...

  10. python3.7使用models.ForeignKey时一定要传入实参on_delete=models.CASCADE

    models.ForeignKey 模型中最重要的部分——以及模型中唯一需要的部分——是它定义的数据库字段列表.字段由类属性指定.注意不要选择与模型API冲突的字段名称,如清除.保存或删除. from ...