在布局

时间:2015-05-29 14:14:12

标签: android android-layout notifications android-custom-view

当我在通知的布局中使用自定义视图时,我会崩溃RemoteServiceException: Bad notification posted from package ...。这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.myapp.views.FontedTextView
    android:id="@+id/idas"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="test" />
</LinearLayout>

如果我使用TextView代替com.myapp.views.FontedTextView,则可以正常使用。请注意,在正常活动中,com.myapp.views.FontedTextView效果很好。

public class FontedTextView extends TextView
{
public FontedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(isInEditMode()) return;
        createFont();
}

public FontedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(isInEditMode()) return;
        createFont();
}

public FontedTextView(Context context) {
        super(context);
        if(isInEditMode()) return;
        createFont();
}

public void createFont()
{
    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "Lato-Regular.ttf");
    setTypeface(font);
}
}

实例:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    RemoteViews remoteviews = new RemoteViews(context.getPackageName(), R.layout.remotecontrol_notification);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContent(remoteviews)
    .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0));

    notificationManager.notify(100, builder.build());

0 个答案:

没有答案