日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

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獲取應用程序的上下文!!!

2、設置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)

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

改變位置后的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();}

代碼運行效果:?

?

轉載于:https://www.cnblogs.com/DreamRecorder/p/9256971.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。