-->

Get TextView from RemoteViews to make its text scr

2019-05-18 18:12发布

问题:

I have a notification built with a RemoteView from a service.

The RemoteView layout contains a TextView that I want to have it scrolling.

I've set it up as marquee in the xml, all good. But now I need to set textView.setSelected(true) therefore I need to somehow reference the textView.

So how do I get the textView from the RemoteView to do setSelected()? Or is there another way to make it scrolling in the notification?

I tried

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.notification, null);
        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setSelected(true);

or

         View v = views.apply(getApplicationContext(), null);
         TextView text = (TextView) v.findViewById(R.id.text);
         text.setSelected(true);

but the text isn't scrolling..

This is my xml:

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:duplicateParentState="true"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="false"
    android:singleLine="true" >

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

回答1:

This is enough:

<TextView 
android:id="@+id/text" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:duplicateParentState="true" 
android:ellipsize="marquee" 
android:fadingEdge="horizontal" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:gravity="center" 
android:marqueeRepeatLimit="marquee_forever" 
android:scrollHorizontally="false" 
android:singleLine="true" > 
    <requestFocus android:duplicateParentState="true" 
        android:focusable="true" 
        android:focusableInTouchMode="true" /> 
</TextView>