Android custom notification layout mit RemoteViews
Ich versuche, erstellen Sie eine benutzerdefinierte Benachrichtigung für meine android-app mit dieser post, und ich stolperte über ein problem, von dem ich versuchte zu lösen, für die letzten 2 Stunden.
Nur die Bildansicht von meinem layout zeigt, und ich kann nicht herausfinden, wie zu machen es funktioniert wie vorgesehen.
Mein code:
package be.ac.ucl.lfsab1509.cumulus.services;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.widget.RemoteViews;
import be.ac.ucl.lfsab1509.cumulus.R;
import be.ac.ucl.lfsab1509.cumulus.activities.MainActivity;
import be.ac.ucl.lfsab1509.cumulus.entities.Song;
public class CumulusNotification {
private CumulusService mCtx;
private CumulusMediaPlayer mPlayer;
private Notification mNotification;
private NotificationCompat.Builder mBuilder;
private RemoteViews mContentView;
public CumulusNotification(CumulusService ctx, CumulusMediaPlayer player){
mCtx = ctx;
mPlayer = player;
mBuilder = new NotificationCompat.Builder(
mCtx);
mBuilder.setSmallIcon(R.drawable.ic_stat_cumulus_notification);
//mBuilder.setContentTitle("Cumulus is playing");
//mBuilder.setTicker("Cumulus");
//mBuilder.setContentText(mCtx.getCurrentSong().title());
Intent resultIntent = new Intent(mCtx, MainActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
resultIntent.setAction(Intent.ACTION_MAIN);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mCtx);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
mContentView = new RemoteViews(mCtx.getPackageName(), R.layout.statusbar);
mContentView.setImageViewResource(R.id.notifimage, R.drawable.cumulus_icon);
mContentView.setTextViewText(R.id.notiftitle, "Custom notification");
mContentView.setTextViewText(R.id.notiftext, "This is a custom layout");
mBuilder.setContent(mContentView);
}
public void startNotification() {
mNotification = mBuilder.build();
//mNotification.contentView = mContentView;
mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
public void updateSong(Song song){
mBuilder.setContentText(song.title());
mNotification = mBuilder.build();
mCtx.startForeground(R.integer.NOTIFICATION_ID, mNotification);
}
}
Der xml-Datei:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:gravity="left" >
<ImageView android:id="@+id/notifimage"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/notiftitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notifimage" />
<TextView android:id="@+id/notiftext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/notifimage"
android:layout_below="@id/notiftitle" />
</RelativeLayout>
Danke für die Hilfe
Wenn Sie nach der statusbar.xml das würde helfen, fixieren Sie das layout viel einfacher...
Fertig! Wie ich schon sagte, wie in der original-post
Sie waren in der Lage, es zu lösen ? Ich stehe vor demselben Problem. Thx
Warum haben Sie
Fertig! Wie ich schon sagte, wie in der original-post
Sie waren in der Lage, es zu lösen ? Ich stehe vor demselben Problem. Thx
Warum haben Sie
android:layout_toRightOf="@id/notifimage"
auf beiden TextViews?InformationsquelleAutor HappyRave | 2014-04-22
Du musst angemeldet sein, um einen Kommentar abzugeben.
Problem ist Sie haben nicht die richtige contentView.
Aber Ihre remote-Ansicht ist mContentView. ändern Sie es zu
Hier ist ein Beispiel
Haben Sie versucht, mein Beispiel code
Auch, entfernen Sie den Stil und layout, was, die Sie für TextView. es muss eine gültige Stil, wenn erforderlich
InformationsquelleAutor Libin
Können Sie sehen, Beispiel hier:
http://www.laurivan.com/android-notifications-with-custom-layout/
Denke ich, hinzufügen von
Ihre builder können das problem lösen.
InformationsquelleAutor Artem Kleinschmidt
Ich denke, dies ist nur ein layout-Problem, je mehr ich darüber nachdenke. Ich denke, dass, wenn Sie wurden zu setzen
android:layout_alignParentRight="true"
auf dieTextView
s, Sie würden bekommen, was Sie gesucht haben. Es sieht so aus, als legte Sie nach rechts von derImageView
, aber nicht geben Ihnen jede Breite im layout der Zeit, da Sie nicht bitten für alle, und Sie haben noch keine Inhalte.Wenn das nicht funktioniert, lassen Sie es uns wissen!
InformationsquelleAutor Travis
Diese post erklärt sehr cool.
Grundsätzlich und so:
InformationsquelleAutor Douglas Mesquita