首页 > 解决方案 > Marquee 无法在 Android Oreo 上运行,我该怎么办?

问题描述

我正在构建一个音乐应用程序,当播放任何歌曲时,应用程序将显示一个通知播放器,包括图像、艺术家姓名和标题歌曲(我为此使用选取框效果)。大多数设备的 OS 4/5/6/7 都很好,除了在 OS 8 上之外,标题歌曲可以以选取框效果运行。有什么问题?我该怎么办?

这是 notification_player.ics.xml :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@id/notify_image_jacket"
    android:layout_alignParentRight="true"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/notify_image_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/image_description"
            android:src="@drawable/ic_notify_play" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="24dp"
            android:layout_weight="1"
            android:orientation="horizontal" >
            <!-- #1242 - Add title color white-->
            <TextView
                android:id="@+id/notify_text_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:textColor="@color/font_color_white"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:duplicateParentState="true">
                <requestFocus android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:duplicateParentState="true"/>
            </TextView>

            <TextView
                android:id="@+id/notify_text_separator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/separator"
                android:textColor="@color/font_color_white" />

            <!-- #1242 - Add title color white-->
            <TextView
                android:id="@+id/notify_text_artist"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:singleLine="true"
                android:textColor="@color/font_color_white" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

我正在使用 RemoteViews 和通知通道

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        views = new RemoteViews(getPackageName(), R.layout.notification_player_ics);
    }
    views.setTextViewText(R.id.notify_text_title, LibraryUtility.getDispTrackName(mContext, title));
    views.setTextViewText(R.id.notify_text_artist, LibraryUtility.getDispArtistName(mContext, artist));

    /**#2178 - Edit - Issue for media notifications to support Android Oreo - start */
    String channelid = String.valueOf(NotificationConst.NOTIFICATION_MEDIASERVICE);

    int pFlag = PendingIntent.FLAG_UPDATE_CURRENT;
    //PLUS_MUSE-1194 【SCL23】通知エリアを押下した時にアプリが復帰しない対応
    if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
        //4.4の一部端末でFLAG_UPDATE_CURRENTが使用できないためFLAG_CANCEL_CURRENTで代用
        pFlag = PendingIntent.FLAG_CANCEL_CURRENT;
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, pFlag);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getResources().getString(R.string.app_name);
        Utility.createChannelNotifications(mNotificationManager, NotificationManager.IMPORTANCE_LOW, name, channelid);
    }

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        if (mNotification == null) {
            mNotification = new Notification();
        }
        mNotification.contentView = views;
        mNotification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
        mNotification.icon = R.drawable.ic_notify_play;
        mNotification.contentIntent = pendingIntent;
    } else {
        if (mBuilder == null) {
            mBuilder = new NotificationCompat.Builder(getApplicationContext(), channelid);
        }
        mBuilder.setSmallIcon(R.drawable.ic_notify_play);
        mBuilder.setCustomContentView(views);
        //mBuilder.setContent(views);
        mBuilder.setAutoCancel(false);
        mBuilder.setOngoing(true);
        mBuilder.setColor(ContextCompat.getColor(mContext, R.color.bg_color_black));
        mBuilder.setContentIntent(pendingIntent);
    }

这是 createChannelNotifications() 方法:

public static void createChannelNotifications(NotificationManager notificationManager, int importance, CharSequence name, String channelid) {
    String id = channelid;
    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
    mChannel.enableLights(true);
    mChannel.enableVibration(true);
    mChannel.setLightColor(Color.GREEN);
    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    mChannel.setVibrationPattern(new long[]{0});
    if (notificationManager != null) {
        notificationManager.createNotificationChannel(mChannel);
    }
}

标签: androidnotificationsmarquee

解决方案


尝试在奥利奥中使用它对我有用....

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="#55000000"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:freezesText="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:padding="5dp"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:textStyle="bold" />

推荐阅读