Android Notification TextView Marquee Do Not Work?

放肆的年华 提交于 2020-01-05 08:53:13

问题


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

回答1:


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) {  
}  

}



来源:https://stackoverflow.com/questions/18704319/android-notification-textview-marquee-do-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!