歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android之Toast的用法

Android之Toast的用法

日期:2017/3/1 11:17:15   编辑:Linux編程

有些程序可以配置,用來讓用戶設置有些自定義的偏好

Toast是用來向用戶展示一些提示性信息,並且該VIEW永遠不會獲得系統的焦點

一般定義為:Toast.makeText(MyClass.this,"note info",Toast.LENGTH_SHORT).show();

也可以在Tost中顯示出自定義的VIEW,如下所示展示出圖片和文字

自定義的 一個顯示Toast的方法

  1. protected void showToast() {
  2. // create the view
  3. View view = inflateView(R.layout.incoming_message_panel);
  4. // set the text in the view
  5. TextView tv = (TextView)view.findViewById(R.id.message);
  6. tv.setText("khtx. meet u for dinner. cul8r");
  7. // show the toast
  8. Toast toast = new Toast(this);
  9. toast.setView(view);
  10. toast.setDuration(Toast.LENGTH_LONG);
  11. toast.show();
  12. }

incoming_message_panel的定義如下所示:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <FrameLayout xmlns:Android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:background="@android:drawable/toast_frame">
  6. <LinearLayout
  7. android:orientation="horizontal"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content">
  10. <ImageView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:src="@drawable/sample_thumb_2"
  14. />
  15. <TextView
  16. android:id="@+id/message"
  17. android:layout_gravity="center_vertical"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:paddingLeft="6dip"
  21. />
  22. </LinearLayout>
  23. </FrameLayout>

toast_frame圖片如下所示


Copyright © Linux教程網 All Rights Reserved