Android學習筆記之Toast

來源:互聯網
上載者:User

<1>簡介

Toast是一個為使用者快速傳遞小訊息的視圖。Toast類可以協助你建立和展現那些訊息。

當視圖顯示給使用者的時候,看起來就像漂浮在應用程式之上。它將不會接收到的滑鼠焦點。會一閃而過。

給你兩個例子:音量控制和說你的設定已經儲存的簡短訊息

 

最簡單的方法是使用這個類調用一個靜態方法,能構建你所需要的一切並且返回一個新Toast對象。

 

<2>建立

首先,執行個體化一個帶有makeText()方法的Toast對象。

該方法帶有三個參數:應用程式Context,文字資訊,Toast存在時間。

它返回一個正確地初始化Toast對象。你可以利用show()方法顯示這個Toast。

Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show();

 

然而,你可以想位置不同的Toast,甚至用自己的布局,而不是一個簡單的文本資訊。

<3>定位Toast

一個標準的toast notification 會出現在螢幕的底部,水平方向置中。你可以改變這一定位用public    void    setGravity(int gravity, int xOffset, int yOffset)方法。

這個方法需要三個參數:Gravity常量,X方向的位移量,Y方向的位移量;

例如,如果你決定應該出現在左上方,你可以設定gravity像這樣:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

如果你想向右邊推進,你只需要稍微改變一下第二個參數的值就可以了。

如果你想向下邊推進,你只需要稍微改變一下第三個參數的值就可以了。

Gravity常量:開啟查看常量

package xiaosi.toast;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.widget.Toast;public class ToastActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Toast toast = Toast.makeText(this,"祝你新婚快樂",Toast.LENGTH_LONG);        toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);        toast.show();    }}

<4>建立一個定製的Toast

如果一個簡單的文本資訊是不夠的,你可以為你Toast建立一個自訂的布局。

為了建立一個自訂布局,定義一個視圖布局在XML或在你的應用程式代碼,把View對象傳遞給給setView(View)方法。

例如:

建立一個布局:(toast_layout.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                  android:id="@+id/toast_layout_root"                  android:orientation="horizontal"                  android:layout_width="fill_parent"                  android:layout_height="fill_parent"                   android:padding="10dp"                   android:background="#DAAA"                   >         <ImageView          android:id="@+id/image"                        android:layout_width="wrap_content"                        android:layout_height="fill_parent"                        android:layout_marginRight="10dp"                        />        <TextView         android:id="@+id/text"                      android:layout_width="wrap_content"                      android:layout_height="fill_parent"                      android:textColor="#FFF"                      /></LinearLayout>

注意的LinearLayoutID元素是“toast_layout”。

package xiaosi.toast;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;public class ToastActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                LayoutInflater inflater = getLayoutInflater();        View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root));        ImageView image = (ImageView) layout.findViewById(R.id.image);        image.setImageResource(R.drawable.ic_launcher);        TextView text = (TextView) layout.findViewById(R.id.text);                        Toast toast = Toast.makeText(this,"祝你新婚快樂",Toast.LENGTH_LONG);        toast.setGravity(Gravity.CENTER, 0, 0);        toast.setView(layout);        toast.show();    }}

首先,檢索LayoutInflater 通過getLayoutInflater()和(或getSystemService())

然後利用inflate(int,ViewGroup)方法填充XML中的布局。第一參數是布局的資源ID,第二個是一個View。

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.