1.直接調用Toast類的makeText()方法創建
這是我們用的最多的一種形式了!比如點擊一個按鈕,然后彈出Toast,用法: Toast.makeText(MainActivity.this, "提示的內容", Toast.LENGTH_LONG).show(); 第一個是上下文對象!對二個是顯示的內容!第三個是顯示的時間,只有LONG和SHORT兩種 會生效,即時你定義了其他的值,最后調用的還是這兩個!
另外Toast是非常常用的,我們可以把這些公共的部分抽取出來,寫到一個方法里! 需要顯示Toast的時候直接調用這個方法就可以顯示Toast,這樣方便很多! 示例如下:
>
void midToast(String str, int showTime)
{
Toast toast = Toast.makeText(global_context, str, showTime);
toast.tGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL , 0, 0); //設置顯示位置
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.tTextColor(Color.YELLOW); //設置字體顏色
toast.show();
}
上面這個抽取出來的方法,我們發現我們可以調用tGravity設置Toast顯示的位置以及獲得 通過findViewById(android.R.id.message)獲得顯示的文本,然后進行設置顏色,或者大小等! 這就是第二種通過構造方法來定制Toast!
2.通過構造方法來定制Toast:
上面定制了文本,以及顯示位置,下面我們寫兩個簡單的例子:
1.定義一個帶有圖片的Toast
效果圖:
關鍵代碼:
private void midToast(String str, int showTime)
{
Toast toast = Toast.makeText(mContext, str, showTime);
toast.tGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM , 0, 0); //設置顯示位置
LinearLayout layout = (LinearLayout) toast.getView();
layout.tBackgroundColor(Color.BLUE);
ImageView image = new ImageView(this);
image.tImageResource(R.mipmap.ic_icon_qitao);
layout.addView(image, 0);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
v.tTextColor(Color.YELLOW); //設置字體顏色
toast.show();
}
2.Toast完全自定義
如果上面的那種還滿足不了你的話,那么你完全可以自己寫一個Toast的布局,然后顯示出來; 但是時間我們依舊控制不了!
運行效果圖:
關鍵代碼:
private void midToast(String str, int showTime)
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.view_toast_custom,
(ViewGroup) findViewById(R.id.lly_toast));
ImageView img_logo = (ImageView) view.findViewById(R.id.img_logo);
TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
tv_msg.tText(str);
Toast toast = new Toast(mContext);
toast.tGravity(Gravity.CENTER, 0, 0);
toast.tDuration(Toast.LENGTH_LONG);
toast.tView(view);
toast.show();
}
還有自定義Toast的布局以及圓角背景:
圓角背景:bg_toast.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設置透明背景色 -->
<solid android:color="#BADB66" />
<!-- 設置一個黑色邊框 -->
<stroke
android:width="1px"
android:color="#FFFFFF" />
<!-- 設置四個圓角的半徑 -->
<corners
android:bottomLeftRadius="50px"
android:bottomRightRadius="50px"
android:topLeftRadius="50px"
android:topRightRadius="50px" />
<!-- 設置一下邊距,讓空間大一點 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>
布局文件:view_toast_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lly_toast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_toast"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_logo"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_marginLeft="10dp"
android:src="@mipmap/iv_lol_icon1" />
<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="20sp" />
</LinearLayout>
非常簡單,嘿嘿~
3.示例代碼下載
ToastDemo.zip
本節小結:
好的,本節給大家講解了Toast的基本使用,以及如何自定義Toast,非常簡單,大家可以在實際開發中對自己的Toast進行定制
本文發布于:2023-02-28 20:14:00,感謝您對本站的認可!
本文鏈接:http://m.newhan.cn/zhishi/a/167766448479436.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:toast.maketext.doc
本文 PDF 下載地址:toast.maketext.pdf
| 留言與評論(共有 0 條評論) |