1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="100dp"> 5 <ImageView 6 android:id="@+id/poster" 7 android:layout_width="108dp" 8 android:layout_height="match_parent" 9 android:background="@drawable/poster" 10 android:contentDescription="這是專輯圖片" /> 11 <LinearLayout 12 android:layout_width="match_parent" 13 android:layout_height="100dp" 14 android:background="@android:color/white" 15 android:orientation="vertical"> 16 <LinearLayout 17 android:layout_width="match_parent" 18 android:layout_height="50dp" 19 android:orientation="horizontal" 20 android:paddingLeft="9dp" 21 android:paddingRight="9dp" 22 android:paddingTop="9dp"> 23 <TextView 24 android:id="@+id/title" 25 android:layout_width="match_parent" 26 android:layout_height="match_parent" 27 android:layout_weight="1" 28 android:ellipsize="end" 29 android:gravity="center" 30 android:singleLine="true" 31 android:text="從前的我-陳潔儀" 32 android:textColor="#000000" 33 android:textSize="16sp" /> 34 <ImageView 35 android:id="@+id/exit" 36 android:layout_width="64dp" 37 android:layout_height="45dp" 38 android:layout_gravity="center_vertical" 39 android:src="@drawable/exit" /> 40 </LinearLayout> 41 <LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="50dp" 44 android:gravity="center" 45 android:orientation="horizontal"> 46 <ImageView 47 android:id="@+id/left" 48 android:layout_width="0dp" 49 android:layout_height="wrap_content" 50 android:layout_weight="1" 51 android:src="@drawable/left" /> 52 <ImageView 53 android:id="@+id/on" 54 android:layout_width="0dp" 55 android:layout_height="wrap_content" 56 android:layout_weight="1" 57 android:src="@drawable/on" /> 58 <ImageView 59 android:id="@+id/right" 60 android:layout_width="0dp" 61 android:layout_height="wrap_content" 62 android:layout_weight="1" 63 android:src="@drawable/right" /> 64 <ImageView 65 android:id="@+id/love" 66 android:layout_width="0dp" 67 android:layout_height="wrap_content" 68 android:layout_weight="1" 69 android:src="@drawable/love" /> 70 </LinearLayout> 71 </LinearLayout> 72 </LinearLayout>
1 // 一、建立 manger 2 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 3 // 二、 建立 NotificationChannel(通知渠道) android 新特性 4 NotificationChannel channel = new NotificationChannel("1", 5 "Channel1", NotificationManager.IMPORTANCE_DEFAULT); 6 channel.enableLights(true); //是否在桌面icon右上角展現小紅點 7 channel.setLightColor(Color.GREEN); //小紅點顏色 8 channel.setShowBadge(true); //是否在久按桌面圖標時顯示此渠道的通知 9 notificationManager.createNotificationChannel(channel); 10 // 三、 建立 Builder 11 NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,"1"); 12 builder.setSmallIcon(R.drawable.music); 13 builder.setContentTitle("title"); 14 builder.setContentText("content title"); 15 // builder.setNumber(3); //久按桌面圖標時容許的此條通知的數量 16 // 四、 綁定 layout 17 RemoteViews rv = new RemoteViews(getPackageName(),R.layout.customnotice);
/* 更改視圖內容 */ 18 //rv.setTextViewText(R.id.title,"泡沫");//修改自定義View中的歌名 19 //修改自定義View中的圖片(兩種方法) 20 //rv.setImageViewResource(R.id.iv,R.mipmap.ic_launcher); 21 // rv.setImageViewBitmap(R.id.poster, BitmapFactory.decodeResource(getResources(),R.drawable.music));
22 builder.setContent(rv); 23 // 五、 建立 notification 24 Notification notification = builder.build(); 25 // 六、發送通知 26 notificationManager.notify(0,notification); 27 28 // //刪除NotificationChannel 29 // NotificationChannel mChannel =manager.getNotificationChannel(id); 30 // manager.deleteNotificationChannel(mChannel);