首页 文章

Android:在通知中更新文本而不重置动画

提问于
浏览
0

我正在尝试为我的音乐应用实施通知 . 它由播放/暂停和下一个按钮以及启用了选框的TextView组成,以便歌曲 Headers 滚动 . 当我尝试更改播放/暂停按钮图标时,选取框文本将重置 .

这是我的通知布局文件:

<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>

通知类:

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重置它的位置 . 有没有办法在不重置整个布局的情况下更改图标?

1 回答

  • 1

    试试这个:

    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());
    }
    

相关问题