日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用Toast进行用户提醒(转)

發(fā)布時間:2025/7/25 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Toast进行用户提醒(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Toast是Android提供的一個輕量級的用戶提醒控件,使用也很簡單,就相當一個極簡的dialog!!!下面將向您介紹一些Toast的詳細用法:

1、普遍使用的方法:

Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.show(); 一般情況下,我們都是這樣使用Toast的,就跟其他的UI一樣,初始化一個UI需要傳入一個Context,這里是通過getApplicationContext獲取應(yīng)用程序的上下文!!!

2、設(shè)置Toast顯示的位置:?
一般情況下,Toast顯示在屏幕的下半屏幕中,就像下圖所示的那樣:?

我們可以通過代碼更新Toast顯示的位置:

Context context = getApplicationContext();CharSequence text = "Hello toast!";int duration = Toast.LENGTH_SHORT;Toast toast = Toast.makeText(context, text, duration);toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);toast.show();

?

方法原型:

public void setGravity(int gravity, int xOffset, int yOffset)

這里的參數(shù)意義就不介紹,相信您根據(jù)名字就可以猜出來!!!

改變位置后的Toast:?

3、自定義Toast的Layout:?
Toast的布局如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/custom_toast_container"android:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="8dp"android:background="#DAAA"><ImageView android:src="@drawable/droid"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="8dp"/><TextView android:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFF"/></LinearLayout>

在代碼中解析layout,并將解析的布局添加至Toast中,具體代碼如下所示:

public void onShowCustomToast(View view) {LayoutInflater inflater = getLayoutInflater();View layout = inflater.inflate(R.layout.toast_layout,null);TextView text = (TextView) layout.findViewById(R.id.text);text.setText("This is a custom toast");Toast toast = new Toast(getApplicationContext());toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);toast.setDuration(Toast.LENGTH_LONG);toast.setView(layout);toast.show();}

代碼運行效果:?

?

轉(zhuǎn)載于:https://www.cnblogs.com/DreamRecorder/p/9256971.html

總結(jié)

以上是生活随笔為你收集整理的使用Toast进行用户提醒(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。