Android Toast 預設和自定義使用

PM實驗室發表於2016-09-13

Toast是一種簡易的訊息提示框,和Dialog不一樣的是,Toast永遠不會獲得焦點,無法被點選。

Toast的設計思想就是儘可能不太引人注意,同時還能向使用者展示資訊,希望他們看到。

Toast顯示的時間有限,只分為long和short,它會根據使用者設定的顯示時間後自動消失。

1.預設Toast

myToast1("這是預設的Toast");
public void myToast1(String str) {
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
}

引數依次是顯示Toast的上下文、顯示的字串、顯示時間、最後show出來即可。

2.自定義Tosat

先大致瞭解一下LayoutInflater

作用: 
1、對於一個沒有被載入或者想要動態載入的介面, 都需要使用inflate來載入. 
2、對於一個已經載入的Activity, 就可以使用實現了這個Activiyt的的findViewById方法來獲得其中的介面元素. 

方法: 
 1、Android裡面想要建立一個畫面的時候, 初學一般都是新建一個類, 繼承Activity基類, 然後在onCreate裡面使用setContentView方法來載入一個在xml裡定義好的介面. 
 2、其實在Activity裡面就使用了LayoutInflater來載入介面, 通過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以獲得一個 LayoutInflater, 也可以通過LayoutInflater inflater = getLayoutInflater();來獲得.然後使用inflate方法來載入layout的xml, 

區別:
1、 LayoutInflater.inflate是載入一個佈局檔案。

2、 findViewById則是從佈局檔案中查詢一個控制元件;

myToast2("這是PM自定義的Toast");
public void myToast2(String str) {
        LayoutInflater inflater = getLayoutInflater();
        //作用就是將一個xml定義的佈局檔案例項化為view控制元件物件;
        //1.reSource:View的layout的ID
        //2.root:需要附加到resource資原始檔的根控制元件,inflate()會返回一個View物件,
        // 如果第三個引數attachToRoot為true,就將這個root作為根物件返回,
        // 否則僅僅將這個root物件的LayoutParams屬性附加到resource物件的根佈局物件上,也就是佈局檔案resource的最外層的View上。
        // 如果root為null則會忽略view根物件的LayoutParams屬性(注意)。
        //3.attachToRoot:是否將root附加到佈局檔案的根檢視上
        View view = inflater.inflate(
                R.layout.demo11_toast,
                (ViewGroup) findViewById(R.id.R_toast),
                false
        );
        //設定ImageView
        ImageView img_toast = (ImageView) view.findViewById(R.id.img_toast);
        img_toast.setImageResource(R.mipmap.ic_launcher);
        //設定TextView
        TextView tv_msg = (TextView) view.findViewById(R.id.tv_toast);
        tv_msg.setText(str);
        //上下文
        Toast toast = new Toast(getApplicationContext());
        //設定顯示位置,
        toast.setGravity(Gravity.CENTER, 0, 0);
        //設定時間
        toast.setDuration(Toast.LENGTH_LONG);
        //設定View
        toast.setView(view);
        //顯示Tosat
        toast.show();
 }

demo11_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/R_toast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/demo11_bg_toast">

    <ImageView
        android:id="@+id/img_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/img_toast"
        android:textSize="14sp" />

</RelativeLayout>

demo11_bg_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--設定透明背景色-->
    <solid android:color="#3989cd" />
    <!--設定黑色邊框-->
    <stroke
        android:width="1px"
        android:color="#FFFFFF" />
    <!--設定四個圓角半徑-->
    <corners
        android:bottomLeftRadius="50px"
        android:bottomRightRadius="50px"
        android:topLeftRadius="50px"
        android:topRightRadius="50px" />
    <!--設定邊距,讓空間大一點-->
    <padding
        android:bottom="4dp"
        android:left="7dp"
        android:right="7dp"
        android:top="4dp" />
</shape>

好了,基本的Toast就這麼多,有錯誤請指正。

相關文章