Android-基本控制項(Toast 全解)

來源:互聯網
上載者:User

標籤:toast全解   自訂布局實現toast   toast添加圖片   

1.回顧

   上篇學習和使用了RatingBar 和 OnRatingBarChangeListener()

2.重點

   (1)借用RationBar

   (2)預設Toast 實現

   (3)改變位置Toast 實現

   (4)給Toast添加圖片實現

   (5)自訂布局實現Toast

3.Toast 實現   3.1

                                                 

    3.2 說明

     (1) 採用的是 RatingBar 的 OnRatingBarChangeListener 監聽 改變 ,觸發 Toast 的調用;

     (2) Toast 中的文本 可以直接複製,也可以來自 string.xml 檔案

    3.3 預設Toast實現

 

/** * 預設 Toast *  */private void ToastShow(){  Toast toast=Toast.makeText(this,"預設Toast",Toast.LENGTH_SHORT);            toast.show();       //縮減寫法       //Toast.makeText(this,str,Toast.LENGTH_SHORT).show();}
 

    3.4 改變位置的Toast 實現

       只需要設定屬性 setGravity() 即可

      /** * 改變位置的Toast */private void ToastChangeGravity(){  Toast toast=Toast.makeText(this,"改變位置的Toast",Toast.LENGTH_SHORT);            toast.setGravity(Gravity.CENTER,0,0);            toast.show();}

    3.5 添加圖片的Toast 實現

          (1)拿到Toast 的view對象 ,轉換為 LineatLayout 

          (2)動態添加圖片

          (3)看注釋

/** * 添加 圖片的Toast */private void ToastAddImage(){//這裡的文本可以來自 string.xml 通過 id 來 取值 ,也可以 直接是 文本資訊Toast toast=Toast.makeText(this,R.string.toast_addimg, Toast.LENGTH_SHORT);    //使用 LinearLayout 添加 動態圖片LinearLayout linearLayout=(LinearLayout) toast.getView();//建立ImageViewImageView imageView=new ImageView(this);imageView.setImageResource(R.drawable.img);//添加給 Toast//預設的圖片在下面,如果添加 第二個參數的話 :可以設定圖片的位置linearLayout.addView(imageView,0);toast.show();}

    3.6 自訂Toast實現

     (1)自訂Layout布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/img" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_marginTop="30dp"        android:layout_height="wrap_content"        android:text="我是自訂的toastlayout" /></LinearLayout>

    (2)轉換為View對象

            布局檔案轉換為View對象 ,有兩種方法:

          第一種是通過 View.inflate() 實現 :

View view=View.inflate(this,R.layout.toastlayout,null);
       

           第二種 通過 LayoutInflater 對象實現:

                LayoutInflater layoutInflater=getLayoutInflater().from(this);View view=layoutInflater.inflate(R.layout.toastlayout,null);

   (3)執行個體化Toast 設定屬性即可

/** * 完全自訂 Toast  */private void ToastToLayout(){//先將 toastLayout 布局檔案轉換為 view對象View view=View.inflate(this,R.layout.toastlayout,null);//執行個體化 toastToast toast=new Toast(this);//設定布局toast.setView(view);toast.setGravity(Gravity.TOP,0, 0);toast.show();}

   


4. 執行個體demo 免積分下載

    贈送:RatingBar 知識

         http://download.csdn.net/detail/lablenet/9047139



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android-基本控制項(Toast 全解)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.