如何在Android中設計自定義Toast訊息?


在深入瞭解自定義Toast之前,我們應該瞭解Toast是什麼。Toast用於在當前螢幕上顯示訊息一段時間。一段時間後,它會消失。在這個例子中,我們可以學習如何自定義Toast訊息。

此示例演示瞭如何在Android中建立自定義Toast訊息。

步驟1 - 在Android Studio中建立一個新專案,轉到檔案 ⇒ 新建專案,並填寫所有必要的資訊以建立一個新專案。

步驟2 - 將以下程式碼新增到res/layout/activity_main.xml。

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
   xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools =    "http://schemas.android.com/tools" android:layout_width = "match_parent"
   android:layout_height = "match_parent">
   <LinearLayout
      android:layout_width = "match_parent"
      android:layout_height = "match_parent"
      android:background = "#797979"
      android:gravity = "center"
      android:orientation = "vertical">
      <Button
         android:id = "@+id/showToast"
         android:text = "Show Toast"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content" />
   </LinearLayout>
</android.support.constraint.ConstraintLayout>

步驟3 - 將以下程式碼新增到src/MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Button button = findViewById(R.id.showToast);
      button.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            LayoutInflater li = getLayoutInflater();
            View layout = li.inflate(R.layout.custom_toast, (ViewGroup)       findViewById(R.id.custom_toast_layout_id));
            Toast toast = new Toast(getApplicationContext());
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(layout);//setting the view of custom toast layout
            toast.show();
         }
      });
   }
}

步驟4 - 現在在res/layout/ custom_toast.xml中建立自定義Toast佈局,並新增以下程式碼

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/custom_toast_layout_id"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:gravity = "center">
   <LinearLayout
      android:layout_width = "wrap_content"
      android:layout_height = "62dp"
      android:gravity = "center"
      android:background = "@drawable/buttonshape"
      android:orientation = "horizontal">
      <ImageView
         android:id = "@+id/imageView"
         android:layout_width = "100dp"
         android:layout_height = "50dp"
         android:scaleType = "fitStart"
         android:src = "@drawable/logo" />
      <TextView
         android:id = "@id/text"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:layout_marginLeft = "10dp"
         android:layout_marginRight = "20dp"
         android:text = "This is custom toast"
         android:textColor = "#FFF"
         android:textSize = "15sp"
         android:textStyle = "bold" />
   </LinearLayout>
</LinearLayout>

步驟5 - 在上面的程式碼中,我們將佈局的背景設定為drawable中的buttonshape,因此在drawable中建立一個xml檔案作為buttonshape.xml並新增以下程式碼

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android" android:shape = "rectangle" >
   <corners
      android:radius = "14dp"
   />
   <gradient
      android:angle = "45"
      android:centerX = "%"
      android:centerColor = "#47A891"
      android:startColor = "#E8E8E8"
      android:endColor = "#000000"
      android:type = "linear"/>
   <padding
      android:left = "0dp"
      android:top = "0dp"
      android:right = "0dp"
      android:bottom = "0dp"/>
   <size
      android:width = "270dp"
      android:height = "60dp"/>
   <stroke
      android:width = "3dp"
      android:color = "#878787"/>
   </shape>

讓我們嘗試執行您的應用程式。我假設您已將您的實際Android移動裝置連線到您的計算機。要從Android Studio執行應用程式,請開啟專案的一個活動檔案,然後單擊執行 播放圖示 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕

Toast

現在點選“顯示Toast”按鈕,它將給出如下所示的自定義Toast結果

Custom Toast

更新於: 2020年6月26日

890 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告