在Android程序中,Toast可謂用處多多,Toast本身作為消息提示,不占用焦點,用戶可以處理其他程序的同時接收Toast中顯示的信息。但是我們平??匆姷腡oast都是黑框白字的,那么我們可以改變原有的Toast,制作我們自定義的Toast嗎。這個當然可以。Toast類本身提供了定義Toast布局、顯示字體等一些方法。下面以一個例子說明: 新建一個Android工程: activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" tools:context=".MainActivity" > <Button android:id="@+id/showDefineToastButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示簡單的自定義的Toast" /> <Button android:id="@+id/showDefineToastButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示布局自定義Toast" /></LinearLayout>新建一個布局文件用于自定義Toast的布局: toast_view.xml:
<?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_horizontal" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是一個自定義布局的Toast"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/></LinearLayout>簡單的布局,就不介紹了,接下來是MainAcitivty.java:
import android.os.Bundle;import android.app.Activity;import android.graphics.Color;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends Activity { PRivate Button button = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.showDefineToastButton1); button.setOnClickListener(listener); button = (Button) findViewById(R.id.showDefineToastButton2); button.setOnClickListener(listener); } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()) { case R.id.showDefineToastButton1: showDefineToast1(); break; case R.id.showDefineToastButton2: showDefineToast2(); break; } } }; private void showDefineToast1() { Toast toast = Toast.makeText(this, "這是一個簡單的自定義Toast", Toast.LENGTH_SHORT); LinearLayout toastView = (LinearLayout) toast.getView(); toastView.setBackgroundColor(Color.GREEN); toast.show(); } private void showDefineToast2() { LayoutInflater inflater = LayoutInflater.from(this); View toastView = inflater.inflate(R.layout.toast_view, null); // 用布局解析器來解析一個布局去適配Toast的setView方法 Toast toast = new Toast(this); toast.setView(toastView); toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}運行程序,單擊第一個按鈕: 單擊第二個按鈕:
完成,我們成功自定義了我們自己的Toast。
在這里需要注意的是當你使用你自己自定義布局去代替原有Toast布局之后,或者在你用Toast的構造方法構造出一個新的Toast對象的時候,你是不能使用Toast.setText(SequenceChar );方法的,否則程序會崩潰,產生空指針異常。前者是因為Toast.setText();方法不能改變自定義布局文件中的內容,后者是因為剛剛新建出來的Toast對象還沒有設置布局,自然不能添加信息。
如果博客中有什么不對的地方還請多多指點。 謝謝觀看。。。
新聞熱點
疑難解答