標(biāo)題: android里的“吐司” [打印本頁]

作者: xueren    時間: 2013-8-1 16:29
標(biāo)題: android里的“吐司”
兩種方法創(chuàng)建Toast第一種方法的Java代碼:
makeText(Context context, int resId, int duration)

參數(shù):context是toast顯示在哪個上下文,通常是當(dāng)前Activity;resId指顯示內(nèi)容引用Resouce那條數(shù)據(jù),就是從R類中去指定顯示的消息內(nèi)容;duration指定顯示時間,Toast默認(rèn)有LENGTH_SHORT和LENGTH_LONG兩常量,分別表示短時間顯示和長時間顯示。

第二種方法的Java代碼:

makeText(Context context, CharSequence text, int duration)

參數(shù)context和duration與第一個方法相同,參數(shù)text可以自己寫消息內(nèi)容。

用上面任意方法創(chuàng)建Toast對象之后調(diào)用方法show()即可顯示。

Java代碼:

Toast toast = Toast.makeText(ToastDemoActivity.this, "這是一個普通的Toast!", Toast.LENGTH_SHORT);

toast.show();

設(shè)置Toast顯示位置
兩種方法方法可以設(shè)置顯示位置:

方法一:

setGravity(int gravity, int xOffset, int yOffset)三個參數(shù)分別表示(起點位置,水平向右位移,垂直向下位移) 方法二:

setMargin(float horizontalMargin, float verticalMargin) 以橫向和縱向的百分比設(shè)置顯示位置,參數(shù)均為float類型(水平位移正右負(fù)左,豎直位移正上負(fù)下)

Java代碼

// 設(shè)置Toast顯示位置(起點位置,水平向右位移,垂直向下位移)

toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 200);

// Toast顯示位置,以橫向和縱向的百分比計算,參數(shù)均為float類型(水平位移正右負(fù)左,豎直位移正上負(fù)下)

toast.setMargin(-0.5f, 0f);
自定義Toast

下面這段代碼可以顯示一個帶圖片的Toast效果:

<SPAN style="FONT-SIZE: 18px">// 帶圖片的Toast
        Button btn2 = (Button) findViewById(R.id.toast2);
        btn2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // 定義一個Toast
Toast toast = Toast.makeText(ToastDemoActivity.this, "這是一個代圖片的Toast!", Toast.LENGTH_LONG);
               // 定義一個ImageView
               ImageView imageView = new ImageView(ToastDemoActivity.this);
               imageView.setImageResource(R.drawable.icon);
               // 獲得Toast的View
               View toastView = toast.getView();
               // 定義一個Layout,這里是Layout
               LinearLayoutlinear Layout = new LinearLayout(ToastDemoActivity.this);
               linearLayout.setOrientation(LinearLayout.HORIZONTAL);
               // 將ImageView和ToastView合并到Layout中
               linearLayout.addView(imageView);
               linearLayout.addView(toastView);
               // 替換掉原有的ToastView
               toast.setView(linearLayout);
               toast.show();
            }
        });</SPAN><SPAN style="FONT-SIZE: 16px">
</SPAN>

完全自定義代碼如下:
LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom,
     (ViewGroup) findViewById(R.id.llToast));
   ImageView image = (ImageView) layout
     .findViewById(R.id.tvImageToast);
   image.setImageResource(R.drawable.icon);
   TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
   title.setText("Attention");
   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
   text.setText("完全自定義Toast");
   toast = new Toast(getApplicationContext());
   toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
以上就是“吐司"了,很簡單的東西了,高手就不用看了..........







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1