我花了一天时间试图解决这个问题,问题是自定义通知布局没有完全显示,看起来像布局被切断了。代码:
int pendingRequestCode = 0;
int pendingFlag = PendingIntent.FLAG_UPDATE_CURRENT;
RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
Intent previousTrackIntent = new Intent();
previousTrackIntent.setAction(PREVIOUS_TRACK_INTENT_FILTER);
PendingIntent pendingIntentPreviousTrack = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, previousTrackIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_previous,pendingIntentPreviousTrack);
Intent pausePlayIntent = new Intent();
pausePlayIntent.setAction(PAUSE_PLAY_INTENT_FILTER);
PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, pausePlayIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_play,pendingIntentPlay);
Intent nextTrackIntent = new Intent();
nextTrackIntent.setAction(NEXT_TRACK_INTENT_FILTER);
PendingIntent pendingIntentNext = PendingIntent.getBroadcast(getApplicationContext(), pendingRequestCode, nextTrackIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_next,pendingIntentNext);
Intent backToAppFromNotificationIntent = new Intent(this, Home.class);
backToAppFromNotificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendInt = PendingIntent.getActivity(this, 0,
backToAppFromNotificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play)
.setAutoCancel(true)
.setContentIntent(pendInt)
.setCustomBigContentView(layout);
Notification not = builder.build();
musicSrv.startForeground(1, not);
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="250dp"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_media_previous"
android:id="@+id/notification_previous"
android:layout_weight="1"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/play"
android:id="@+id/notification_play"
android:layout_weight="1.34" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_media_next"
android:id="@+id/notification_next"
android:layout_weight="1" />
</LinearLayout>
问题是,通知布局根本没有完全显示: screenshot of emulator's screen
我预期的结果: The planned result: