Android通知TextView的字幕不起作用? [英] Android Notification TextView Marquee Do Not Work?

查看:911
本文介绍了Android通知TextView的字幕不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自定义通知状态栏例如三星Galaxy S3股票音乐播放器即可。我使用的 RemoteViews 自定义布局。但我的TextView组件并不在字幕模式下工作。

我的codeS如下。而我在哪里出错?我花了很多时间来解决这个问题,但我不是运气。

三星Galaxy S3股票的音乐播放器通知栏:

这是我的XML布局code:

<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>    <的LinearLayout
        机器人:layout_width =64dip
        机器人:layout_height =64dip
        机器人:方向=垂直>        < ImageView的
            机器人:ID =@ + ID / imgViewIcon
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:contentDescription =@字符串/ APP_NAME
            机器人:SRC =@绘制/ ic_launcher/>        <的TextView
            机器人:ID =@ + ID / textViewNotifPlaylist
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =底
            机器人:ellipsize =金字招牌
            机器人:可聚焦=真
            机器人:focusableInTouchMode =真
            机器人:marqueeRepeatLimit =marquee_forever
            机器人:单线=真
            机器人:文字=@字符串/ APP_NAME
            机器人:文字颜色=#0094FF>            < requestFocus的/>
        < / TextView的>
    < / LinearLayout中>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =64dip
        机器人:layout_marginLeft =3dip
        机器人:方向=垂直>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:比重=中心
            机器人:方向=横向>            <的ImageButton
                机器人:ID =@ + ID / imgBtnNotif previous
                机器人:layout_width =40dip
                机器人:layout_height =40dip
                机器人:layout_marginRight =20dp
                机器人:背景=@绘制/ button_circle
                机器人:contentDescription =@字符串/ media_ previous
                机器人:SRC =@绘制/ media_btn_ previous/>            <的ImageButton
                机器人:ID =@ + ID / imgBtnNotifPlayPause
                机器人:layout_width =40dip
                机器人:layout_height =40dip
                机器人:背景=@绘制/ button_circle
                机器人:contentDescription =@字符串/ media_play
                机器人:SRC =@绘制/ media_btn_play/>            <的ImageButton
                机器人:ID =@ + ID / imgBtnNotifNext
                机器人:layout_width =40dip
                机器人:layout_height =40dip
                机器人:layout_marginLeft =20dp
                机器人:背景=@绘制/ button_circle
                机器人:contentDescription =@字符串/ media_next
                机器人:SRC =@绘制/ media_btn_next/>
        < / LinearLayout中>        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent>            <的TextView
                机器人:ID =@ + ID / textViewNotifItem
                机器人:layout_width =match_parent
                机器人:layout_height =WRAP_CONTENT
                机器人:单线=真
                机器人:ellipsize =金字招牌
                机器人:marqueeRepeatLimit =marquee_forever
                机器人:scrollHorizo​​ntally =假
                机器人:可聚焦=真
                机器人:focusableInTouchMode =真
                机器人:duplicateParentState =真
                机器人:文字=这是很长的文字,我想这个字幕模式。 >                < requestFocus的
                  机器人:可聚焦=真
                  机器人:focusableInTouchMode =真
                  机器人:duplicateParentState =真/>
            < / TextView的>
        < / LinearLayout中>
    < / LinearLayout中>< / LinearLayout中>

和本我的Java code:

私人无效showNotification()
{
    最后NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);    的PendingIntent piContent = ...;
    PI的PendingIntent previous = ...;
    的PendingIntent piPausePlay = ...;
    的PendingIntent piNext = ...;    NotificationCompat.Builder建设者=新NotificationCompat.Builder(本)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(假)
            .setTicker(这是股票文本)
            .setContentTitle(的getString(R.string.app_name))
            .setContentText(这是文字内容)
            .setContentIntent(piContent);    RemoteViews remoteViews =新的RemoteViews(getPackageName(),R.layout.notification_bar);
    remoteViews.setTextViewText(R.id.textViewNotifPlaylist,样本播放列表);
    remoteViews.setTextViewText(R.id.textViewNotifItem,这是很长的文字,我想这在字幕模式);
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotif previous,PI previous);
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifPlayPause,piPausePlay);
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifNext,piNext);    builder.setContent(remoteViews);    notificationManager.notify(R.string.app_name,builder.build());
}


解决方案

跑马灯需要的TextView获得焦点,所以你可以重载TextView的是这样的:

 公共类MarqueeTextView扩展的TextView {公共MarqueeTextView(上下文安美){
    超(安美);
}公共MarqueeTextView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共MarqueeTextView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
}@覆盖
公共布尔isFocused(){
    返回true;
}@覆盖
保护无效onFocusChanged(布尔重点,诠释方向,
        矩形previouslyFocusedRect){
}

}

I am trying to create custom notification status bar like Samsung Galaxy S3 stock Music Player. I am using RemoteViews for custom layout. But my textview components does not working at marquee mode.

My codes is below. And Where I make mistake? I spent a lot of time to solve this problem, but I am not luck.

Samsung Galaxy S3 stock music player notification bar:

This is my xml layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="64dip"
        android:layout_height="64dip"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imgViewIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textViewNotifPlaylist"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:singleLine="true"
            android:text="@string/app_name"
            android:textColor="#0094FF" >

            <requestFocus />
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="64dip"
        android:layout_marginLeft="3dip"
        android:orientation="vertical" >

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

            <ImageButton
                android:id="@+id/imgBtnNotifPrevious"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_marginRight="20dp"
                android:background="@drawable/button_circle"
                android:contentDescription="@string/media_previous"
                android:src="@drawable/media_btn_previous" />

            <ImageButton
                android:id="@+id/imgBtnNotifPlayPause"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:background="@drawable/button_circle"
                android:contentDescription="@string/media_play"
                android:src="@drawable/media_btn_play" />

            <ImageButton
                android:id="@+id/imgBtnNotifNext"
                android:layout_width="40dip"
                android:layout_height="40dip"
                android:layout_marginLeft="20dp"
                android:background="@drawable/button_circle"
                android:contentDescription="@string/media_next"
                android:src="@drawable/media_btn_next" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/textViewNotifItem"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true" 
                android:ellipsize="marquee"
                android:marqueeRepeatLimit ="marquee_forever"
                android:scrollHorizontally="false"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:duplicateParentState="true"
                android:text="This is the long text and I want to this marquee mode." >

                <requestFocus 
                  android:focusable="true" 
                  android:focusableInTouchMode="true"
                  android:duplicateParentState="true" />
            </TextView>
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

And this my java code:

private void showNotification()
{
    final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent piContent = ...;
    PendingIntent piPrevious = ...;
    PendingIntent piPausePlay = ...;
    PendingIntent piNext = ...;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setAutoCancel(false)
            .setTicker("This is ticker text")
            .setContentTitle(getString(R.string.app_name))
            .setContentText("This is content text")
            .setContentIntent(piContent);

    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_bar);
    remoteViews.setTextViewText(R.id.textViewNotifPlaylist, "Sample Playlist");
    remoteViews.setTextViewText(R.id.textViewNotifItem, "This is long text and I want to this at marquee mode");
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifPrevious, piPrevious);
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifPlayPause, piPausePlay);
    remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifNext, piNext);

    builder.setContent(remoteViews);

    notificationManager.notify(R.string.app_name, builder.build());
}

解决方案

Marquee needs the textview get the focus, so you can overload the textview like this:

public class MarqueeTextView extends TextView {  

public MarqueeTextView(Context appCon) {  
    super(appCon);  
}  

public MarqueeTextView(Context context, AttributeSet attrs) {  
    super(context, attrs);  
}  

public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {  
    super(context, attrs, defStyle);  
}  

@Override  
public boolean isFocused() {  
    return true;  
}  

@Override  
protected void onFocusChanged(boolean focused, int direction,  
        Rect previouslyFocusedRect) {  
}  

}

这篇关于Android通知TextView的字幕不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆