### 1、Toast常用方法:
(1)Toast.makeText(context, text, duration); ? ? ? ? ? ? ? ? ? ? ? ? ? //返回值是Toast
(2)toast.setText(str); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//設置提示內容
(3)toast.setDuration(duration); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//設置持續時間(可以使用默認常量,也可以自己定義)
(4)toast.setGravity(gravity, xOffset, yOffset) ? ? ? ? ? ? ? ? ? ? ? ? //設置toast位置
(5)toast.show(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //顯示
### 2、顯示帶圖片的Toast
~~~
Toast toast=Toast.makeText(this, "click", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toast_Layout=(LinearLayout) toast.getView();
ImageView imageView =new ImageView(this);
imageView.setBackgroundResource(R.drawable.ic_launcher);
toast_Layout.addView(imageView,0); //0是設置圖片在toast_Layout中的位置
toast.show();
~~~
### 3、自定義Toast
toast.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:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="這個是自定義的Toast"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="這是Toast的內容部分!"/>
</LinearLayout>
~~~
主代碼:
~~~
LayoutInflater layoutInflater=LayoutInflater.from(this);
View toast_view=layoutInflater.inflate(R.layout.toast, null);
Toast toast=new Toast(this);
toast.setView(toast_view);
toast.show();
~~~
- 前言
- Java內部類
- 從一個View向一個Activity跳轉
- Android 與 SQLite
- Android工程A依賴B,B依賴C
- Android重要控件概覽(上)
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
- Android布局概覽
- 動態引用APK文件
- Android重要控件概覽(中)
- Android重要控件概覽(下)
- Gallery和ImageSwitcher
- Android之Toast
- Android之Dialog
- Android之Notification
- Android之Menu
- Android Menu中android:showAsAction屬性
- Android SharedPreferences存儲數據的使用方法
- Android手勢識別之GestureDetector
- 不同APP通過SharedPreferences傳遞數據(共享數據)
- 一個自定義的Topbar模板
- 關于Activity回收造成View選中不對應的問題
- Android之Fragment靜態加載