Android: 在不重置动画的情况下更新通知中的文本

Android: Update text in notification without resetting animation

我正在尝试为我的音乐应用实现通知。它由 play/pause 和下一个按钮,以及启用字幕的 TextView 组成,以便歌曲标题滚动。当我尝试更改 play/pause 按钮图标时,选取框文本会重置。

这是我的通知布局文件:

    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/perm_group_audio_settings"
        android:id="@+id/album_art"
        android:background="#00000000" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/album_art"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:fadingEdge="horizontal"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/song_name">
        <requestFocus/>
    </TextView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/album_art"
        android:layout_below="@+id/song_name"
        android:orientation="horizontal"
        android:gravity="right">
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right"
            android:id="@+id/player_stop"
            android:background="#00000000"
            android:src="@drawable/ic_media_stop"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/player_pause"
            android:background="#00000000"
            android:src="@drawable/ic_media_pause"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/player_next"
            android:background="#00000000"
            android:src="@drawable/ic_media_next"/>
    </LinearLayout>
</RelativeLayout>

和通知class:

    public class MediaPlayerNotification extends Notification {
    private Context context;

    private NotificationManager m_manager;
    private NotificationCompat.Builder m_builder;
    private RemoteViews m_view;
    private Notification m_notification;

    public MediaPlayerNotification(Context context) {
        this.context = context;

        m_builder = new NotificationCompat.Builder(context)
                .setContentTitle(NOTIFICATION_TITLE)
                .setSmallIcon(NOTIFICATION_ICON)
                .setOngoing(true)
                .setPriority(Notification.PRIORITY_MAX);

        m_view = new RemoteViews(context.getPackageName(), R.layout.notification_player);
        m_view.setImageViewResource(R.id.player_stop, ICON_STOP);
        m_view.setImageViewResource(R.id.player_pause, ICON_PAUSE);
        m_view.setImageViewResource(R.id.player_next, ICON_NEXT);

        m_builder.setContent(m_view);

        m_notification = m_builder.build();
        m_manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        m_manager.notify(2, m_notification);
    }

    public void setPausePlayIcon(int iconId) {
        m_view.setImageViewResource(R.id.player_pause, iconId);
        m_manager.notify(2, m_notification);
    }
    public void setNowPlaying(String name) {
        m_view.setTextViewText(R.id.song_name, name);
        m_manager.notify(2, m_notification);
    }
}

setPausePlayIcon() 完全按预期工作并更改了图标,但我的 TextView 重置了它的位置。有什么方法可以在不重置整个布局的情况下更改图标?

试试这个:

public void setPausePlayIcon(int iconId) {
    m_view.setImageViewResource(R.id.player_pause, iconId);
    m_builder.setContent(m_view);
    m_manager.notify(2, m_builder.build());
}