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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

android toast几种使用方法 (转)

發布時間:2025/3/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android toast几种使用方法 (转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

toast經常會用到,今天做個總結,特別是自定義toast的布局,值得一看。

一.默認展示

  • // 第一個參數:當前的上下文環境。可用getApplicationContext()或this?
  • // 第二個參數:要顯示的字符串。也可是R.string中字符串ID?
  • // 第三個參數:顯示的時間長短。Toast默認的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms?
  • Toast toast=Toast.makeText(getApplicationContext(), "默認的Toast", Toast.LENGTH_SHORT);?
  • //顯示toast信息?
  • toast.show();?
  • 二.自定義顯示位置

  • Toast toast=Toast.makeText(getApplicationContext(), "自定義顯示位置的Toast", Toast.LENGTH_SHORT);?
  • ??????? //第一個參數:設置toast在屏幕中顯示的位置。我現在的設置是居中靠頂?
  • ??????? //第二個參數:相對于第一個參數設置toast位置的橫向X軸的偏移量,正數向右偏移,負數向左偏移?
  • ??????? //第三個參數:同的第二個參數道理一樣?
  • ??????? //如果你設置的偏移量超過了屏幕的范圍,toast將在屏幕內靠近超出的那個邊界顯示?
  • ??????? toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);??
  • ??????? //屏幕居中顯示,X軸和Y軸偏移量都是0?
  • ??????? //toast.setGravity(Gravity.CENTER, 0, 0); ?
  • ??????? toast.show();?
  • 三、帶圖片的

  • oast toast=Toast.makeText(getApplicationContext(), "顯示帶圖片的toast", 3000);?
  • ??????? toast.setGravity(Gravity.CENTER, 0, 0);??
  • ??????? //創建圖片視圖對象?
  • ??????? ImageView imageView= new ImageView(getApplicationContext());?
  • ??????? //設置圖片?
  • ??????? imageView.setImageResource(R.drawable.ic_launcher);?
  • ??????? //獲得toast的布局?
  • ??????? LinearLayout toastView = (LinearLayout) toast.getView();?
  • ??????? //設置此布局為橫向的?
  • ??????? toastView.setOrientation(LinearLayout.HORIZONTAL);?
  • ??????? //將ImageView在加入到此布局中的第一個位置?
  • ??????? toastView.addView(imageView, 0);?
  • ??????? toast.show();?
  • 四、完全自定義顯示方式

    1.?toast.xml布局

    <ImageViewandroid:id="@+id/image"android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> toast.xml

    2.代碼

    public class ToastActivity extends Activity {private Button bt; private ImageView image; private TextView title, content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showToast(); } }); } private void showToast() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.toast, null); image = (ImageView) view.findViewById(R.id.image); title = (TextView) view.findViewById(R.id.title); content = (TextView) view.findViewById(R.id.content); image.setBackgroundResource(R.drawable.ic_launcher); title.setText("自定義toast"); content.setText("hello,self toast"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); } }

    ?

    五、其他線程通過Handler的調用

    //調用方法1 //Thread th=new Thread(this); //th.start(); //調用方法2 handler.post(new Runnable() { @Override public void run() { showToast(); } }); Java代碼 public void showToast(){ Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他線程中調用顯示", Toast.LENGTH_SHORT); toast.show(); } Java代碼 Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { int what=msg.what; switch (what) { case 1: showToast(); break; default: break; } super.handleMessage(msg); } }; Java代碼 @Override public void run() { handler.sendEmptyMessage(1); }

    轉載于:https://www.cnblogs.com/jianmang/articles/4890888.html

    總結

    以上是生活随笔為你收集整理的android toast几种使用方法 (转)的全部內容,希望文章能夠幫你解決所遇到的問題。

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