1. 程式人生 > >android 頂部訊息通知和Toast頂部彈窗訊息提醒

android 頂部訊息通知和Toast頂部彈窗訊息提醒

1.頂部通知欄的訊息通知

 Intent intent = new Intent(mContext, 跳轉到哪個Activity.class); //點選了之後進入的一個Actity
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification noti = new Notification.Builder(mContext)
                .setTicker
("名稱") .setContentTitle("標題") .setContentText("時間") .setSmallIcon(R.drawable.app_icon)//設定圖示 .setAutoCancel(true)//點選之後消失 .setDefaults(Notification.DEFAULT_SOUND)//設定聲音 .setContentIntent(pendingIntent)//點選之後的頁面 .build
(); NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1,noti);//id控制第幾條,相同的id會替換掉上一次的訊息通知

2.toast彈窗通知

1.主程式碼
        View layoutView =  LayoutInflater.from(context).inflate(R.layout.toast_top, null);
        //設定文字的引數 設定載入文字檔案的引數,必須通過LayoutView獲取。
        TextView timeView = layoutView.findViewById
(R.id.tv_assist_toast_time); TextView contentView = layoutView.findViewById(R.id.tv_assist_toast_content); TextView titletView = layoutView.findViewById(R.id.tv_assist_toast_title); timeView.setText("時間"); titletView.setText("頭部提示"); contentView.setText("訊息內容"); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //獲得螢幕的寬度 int width = wm.getDefaultDisplay().getWidth(); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT); //設定TextView的寬度為 螢幕寬度 layoutView.setLayoutParams(layoutParams); //獲得螢幕的寬度 //建立toast物件, Toast toast = new Toast(context); //把要Toast的佈局檔案放到toast的物件中 toast.setView(layoutView); toast.setDuration(toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 0, 0); toast.getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);//設定Toast可以佈局到系統狀態列的下面 toast.show();
2.通知頁面的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="10dp"
        android:background="@drawable/app_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/tv_assist_toast_title"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:textStyle="bold"
                android:text="遠端協助申請"
                android:layout_gravity="center"
                android:textSize="18dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/tv_assist_toast_time"
                android:layout_width="wrap_content"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:layout_gravity="center"
                android:textStyle="bold"/>

        </LinearLayout>

        <TextView
            android:id="@+id/tv_assist_toast_content"
            android:layout_width="match_parent"
            android:layout_height="20dp"/>

    </LinearLayout>



</LinearLayout>