基于自定義Toast全面解析
Toast一般用來顯示一行文字,用法比較固定:
Toast.makeText(Context context,String message,int duration);
但是有時(shí)候想用toast 來顯示復(fù)雜的view甚至是帶有圖片的view時(shí)這時(shí)候就要用到自定義的Toast,自定義Toast主要用到一下幾個(gè)方法如圖:
1.setView()方法用來顯示用戶自定義的view.
2. setGravity()用來確定Toast顯示的位置.
3.setDuration()用來設(shè)置Toast顯示的時(shí)間長短,只有兩種選擇,LENGTH_SHORT,LENGTH_LONG,都是int型。
4.setText()用來顯示一段文字,但是要注意的時(shí)setText()與setView(),不能同時(shí)使用不然就會(huì)出錯(cuò)。
下面通過一個(gè)完整的代碼來看看如何實(shí)現(xiàn)一個(gè)自定義的Toast:
Toast_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tianjia_p" android:layout_gravity="center" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world" android:layout_gravity="center_horizontal" android:textSize="20dp" /> </LinearLayout>
Activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/myButton" android:text="button" /> </LinearLayout>
ToastUtil:
public class ToastUtil { private static Toast toast; public static void showToast(int duration,View mview,Context context) { if (toast==null) { toast = new Toast(context); } toast.setDuration(duration); toast.setView(mview); toast.show(); } }
MainActivity:
public class MainActivity extends AppCompatActivity { private View toast_view; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LayoutInflater inflater=LayoutInflater.from(this); toast_view =inflater.inflate(R.layout.toast_view,null); button=(Button)findViewById(R.id.myButton); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtil.showToast(Toast.LENGTH_SHORT,toast_view,getApplicationContext()); } }); } }
上面的ToastUtil工具類 寫的不好,大家可以自己改寫自己的ToastUtil類。
以上這篇基于自定義Toast全面解析就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android基礎(chǔ)知識(shí)之frame動(dòng)畫效果
Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果,Android一共提供了兩種動(dòng)畫,這篇文章主要介紹了Android動(dòng)畫效果之frame動(dòng)畫,感興趣的小伙伴們可以參考一下2016-06-06Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼
這篇文章主要介紹了Android 活動(dòng)條ActionBar的詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間
這篇文章主要為大家詳細(xì)介紹了Android Notification實(shí)現(xiàn)動(dòng)態(tài)顯示通話時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí)
這篇文章主要介紹了Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android如何實(shí)現(xiàn)APP自動(dòng)更新
現(xiàn)在一般的android軟件都是需要不斷更新的,當(dāng)你打開某個(gè)app的時(shí)候,如果有新的版本,它會(huì)提示你有新版本需要更新。該小程序?qū)崿F(xiàn)的就是這個(gè)功能。有需要的朋友們可以參考借鑒。2016-08-08Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法,實(shí)例總結(jié)了屏幕旋轉(zhuǎn)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04Android自定義View彈性滑動(dòng)Scroller詳解
這篇文章主要為大家詳細(xì)介紹了Android自定義View彈性滑動(dòng)Scroller,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android videoview搶占焦點(diǎn)的處理方法
這篇文章主要為大家詳細(xì)介紹了Android videoview搶占焦點(diǎn)的處理方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06