自定义通知跑马灯文本在安卓上无法正常工作

4

嗨,我正在尝试在通知管理器中使我的一部分文本滚动,但是没有成功,我尝试了以下方法:

  <TextView
    android:id="@+id/title_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:gravity="bottom"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"                                     
    android:focusable="true"
    android:focusableInTouchMode="true"              
    android:singleLine="true"
    android:textSize="15sp"
    android:textStyle="bold" >
  </TextView>

我在我的.java文件中使用RemoteView:

Notification notification = new Notification(icon, tickerText, when);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);

contentView.setTextViewText(R.id.title_text, "This is a very long text which is not fitting in the screen so it needs to be marqueed");

我希望标题文本能够滚动,这个文本很长,不适合在屏幕上显示,所以需要用到 marquee 特效

但是现在 marquee 特效不能正常工作,只是静止不动,我该怎么办?

谢谢。

3个回答

11

我改了我的 XML:

  <TextView
    android:id="@+id/title_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:gravity="bottom"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="false"                                     
    android:focusable="true"
    android:focusableInTouchMode="true"              
    android:singleLine="true"
    android:textSize="15sp"
    android:textStyle="bold" 
    android:textColor="#fff">
    <requestFocus/> 
  </TextView>
我添加了<requestFocus/>后,现在我的跑马灯效果完美运作。

只是因为我很好奇,你能否尝试将scrollHorizontally设置为true并查看是否有效? - toadzky
如果我将其设置为true,它就可以工作,但是如果我删除<requestFocus/>,它根本不起作用。 - alexistkd
这非常有用。我尝试解决这个问题已经四天了 :-D - Amintabar
从我所了解的情况来看,滚动字幕只有在目标文本字段被选中时才起作用。这就是为什么这个代码能够正常工作的原因 :) - Lo-Tan
它确实可以工作,但我需要尝试扩展通知以便开始,即使使用了requestFocus。 - CésarQ

1
根据文档,将scrollHorizontally属性设置为true不会在边缘处换行。尝试将其设置为false,以便使用ellipsize属性。

1
不起作用 :( 我将其设置为 false,但是跑马灯仍然不工作。 - alexistkd

0

我浪费了一整天才意识到跑马灯效果是通过NotificationCompat.Builder中的setTicker方法实现的,该方法支持RemoteView。

我真心希望这个答案能够拯救许多人的生命 :) (我差点就要把头发都掉光了)


不要在使用.setCustomBigContentView(remoteViewsSetFromCustomLayout)时返回已翻译的文本。 - me_
/**
  • 设置“ticker”文本,该文本将发送到辅助功能服务。在{@link Build.VERSION_CODES#LOLLIPOP}之前,
  • 设置通知首次到达时显示在状态栏中的文本,以及可能在某些设备上显示的RemoteViews对象。 */
- me_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接