Android開發系列(二十三):實現帶圖片提示的Toast提示資訊框

來源:互聯網
上載者:User

標籤:toast

Android中的Toast是很常見的一個訊息提示框,但是預設的訊息提示框就是一行純文字,所以我們可以為它設定一些其他的諸如是帶片的訊息提示。

實現這個很簡單:

就是定義一個Layout視圖,然後設定Toast顯示自訂的View。

在這裡,就是設定了一個LinearLayout容器,然後給這個容器添加圖片,添加文字資訊。然後把這個容器設定給Toast對象,讓其顯示出來。


首先建立一個Android項目,然後我們編輯下main.xml檔案:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:gravity="center_horizontal"><Button  android:id="@+id/simple"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示簡單提示"/><Button  android:id="@+id/bn"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="顯示帶圖片的提示"/></LinearLayout></span>
這裡定義了兩個按鈕,一個是預設的Toast訊息提示,另外一個是顯示帶圖片的資訊提示。


接下來,我們就可以編輯主介面的java代碼了:ToastTest.java

<span style="font-size:14px;">import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;public class ToastTest extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);Button simple = (Button) findViewById(R.id.simple);// 為按鈕的單擊事件綁定事件監聽器simple.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 建立一個Toast提示資訊Toast toast = Toast.makeText(ToastTest.this, "簡單的提示資訊"// 設定該Toast提示資訊的期間, Toast.LENGTH_SHORT);toast.show();}});Button bn = (Button) findViewById(R.id.bn);// 為按鈕的單擊事件綁定事件監聽器bn.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View source){// 建立一個Toast提示資訊Toast toast = new Toast(ToastTest.this);// 設定Toast的顯示位置toast.setGravity(Gravity.CENTER, 0, 0);// 建立一個ImageViewImageView image = new ImageView(ToastTest.this);image.setImageResource(R.drawable.tools);// 建立一個LinearLayout容器LinearLayout ll = new LinearLayout(ToastTest.this);// 向LinearLayout中添加圖片、原有的Viewll.addView(image);// 建立一個ImageViewTextView textView = new TextView(ToastTest.this);textView.setText("帶圖片的提示信");// 設定文字框內字型的大小和顏色textView.setTextSize(30);textView.setTextColor(Color.MAGENTA);ll.addView(textView);// 設定Toast顯示自訂Viewtoast.setView(ll);// 設定Toast的顯示時間toast.setDuration(Toast.LENGTH_LONG);toast.show();}});}}</span>


我們可以得到下邊的:




Android開發系列(二十三):實現帶圖片提示的Toast提示資訊框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.