Marquee TextView not working in Android Layout

二次信任 提交于 2019-12-13 20:16:39

问题


I'm trying to get @+id/trackname and @+id/artist_and_album to use the marquee setting in the following layout but they won't scroll. I also tried using setSelected(true) and setFocusable(true) in the actual Activity but that didn't work. Here is a picture of the layout:

and the XML:

<LinearLayout
    android:id="@+id/player_actionbar"
    android:layout_width="fill_parent"
    android:layout_height="48dip"
    android:layout_alignParentTop="true"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/close"
        android:layout_width="56dip"
        android:layout_height="fill_parent"
        android:padding="4dip"
        android:scaleType="centerInside"
        android:background="@drawable/bg_actionbar_item"
        android:src="@drawable/icn_close" />

    <View
        android:layout_width="1dip"
        android:layout_height="26dip"
        android:background="#ff444444" />

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:textSize="18sp"
        android:textColor="#ffffffff"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:singleLine="true"
        android:layout_marginLeft="8dip"
        android:text="@string/nowplaying_title" />

    <ImageButton
        android:id="@+id/play_queue"
        android:layout_width="56dip"
        android:layout_height="fill_parent"
        android:padding="4dip"
        android:scaleType="centerInside"
        android:background="@drawable/bg_actionbar_item"
        android:src="@drawable/icn_play_queue" />

</LinearLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/player_controller"
    android:layout_below="@+id/player_actionbar" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/player_info"
        android:padding="10dip"
        android:background="@null" >

        <ImageView
            android:id="@+id/album_art"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@null"
            android:src="@drawable/albumart_mp_unknown" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/player_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dip"
        android:layout_marginLeft="12dip"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/track_number_text"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:paddingTop="5dip"
            android:textSize="13dip"
            android:textStyle="normal"
            android:textColor="#ffa9a9a9"
            android:singleLine="true" />

        <TextView 
            android:id="@+id/trackname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17dip"
            android:textStyle="bold"
            android:textColor="#ffffffff"
            android:ellipsize="marquee"
            android:singleLine="true" />

        <TextView
            android:id="@+id/artist_and_album"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13dip"
            android:textStyle="normal"
            android:textColor="#ffa9a9a9"
            android:ellipsize="marquee"
            android:singleLine="true" />

    </LinearLayout>

</RelativeLayout>

<LinearLayout
    android:id="@+id/player_controller"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/position_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="6dip" 
            android:textSize="12dip"
            android:textColor="#ffbbbbbb"
            android:gravity="right" />

        <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="9dip"
            android:paddingRight="9dip"
            android:progress="0"
            android:progressDrawable="@drawable/player_progress_drawable"
            android:thumb="@drawable/player_progress_thumb"
            android:thumbOffset="9dip"
            android:maxHeight="2dip"
            android:minHeight="2dip" />

        <TextView
            android:id="@+id/duration_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="6dip"
            android:textSize="12dip"
            android:textColor="#ffbbbbbb"
            android:gravity="left" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/shuffle_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingBottom="20dip"
            android:paddingLeft="12dip"
            android:paddingRight="10dip"
            android:paddingTop="8dip"
            android:background="@null"
            android:src="@drawable/btn_player_shuffle_normal" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@+id/repeat_button"
            android:layout_toRightOf="@+id/shuffle_button"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <net.sourceforge.servestream.button.RepeatingImageButton
                android:id="@+id/previous_button"
                android:background="@null"
                android:paddingBottom="20dip"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/btn_player_prev" />

            <ImageButton
                android:id="@+id/play_pause_button"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingBottom="20dip"
                android:background="@null"
                android:src="@drawable/btn_player_play" />

            <net.sourceforge.servestream.button.RepeatingImageButton
                android:id="@+id/next_button"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingBottom="20dip"
                android:background="@null"
                android:src="@drawable/btn_player_next" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/repeat_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:paddingBottom="20dip"
            android:paddingLeft="10dip"
            android:paddingRight="12dip"
            android:paddingTop="8dip"
            android:background="@null"
            android:src="@drawable/btn_player_repeat_normal" />

    </RelativeLayout>

</LinearLayout>


回答1:


I modified the layout to:

<LinearLayout
    android:id="@+id/player_actionbar"
    android:layout_width="fill_parent"
    android:layout_height="48dip"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/close"
        android:layout_width="56dip"
        android:layout_height="fill_parent"
        android:padding="4dip"
        android:scaleType="centerInside"
        android:background="@drawable/bg_actionbar_item"
        android:src="@drawable/icn_close" />

    <View
        android:layout_width="1dip"
        android:layout_height="26dip"
        android:background="#ff444444" />

    <TextView
        android:id="@+id/actionbar_title"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:textSize="18sp"
        android:textColor="#ffffffff"
        android:ellipsize="marquee"
        android:gravity="center_vertical"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:singleLine="true"
        android:layout_marginLeft="8dip"
        android:text="@string/nowplaying_title" />

    <ImageButton
        android:id="@+id/play_queue"
        android:layout_width="56dip"
        android:layout_height="fill_parent"
        android:padding="4dip"
        android:scaleType="centerInside"
        android:background="@drawable/bg_actionbar_item"
        android:src="@drawable/icn_play_queue" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:padding="10dip"
        android:background="@null" >

        <net.sourceforge.servestream.utils.CoverView
            android:id="@+id/album_art"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/player_info"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/track_number_text"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:paddingTop="5dip"
            android:textSize="13sp"
            android:textStyle="normal"
            android:textColor="#ffa9a9a9"
            android:singleLine="true" />

        <TextView 
            android:id="@+id/trackname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:textStyle="bold"
            android:textColor="#ffffffff"
            android:ellipsize="marquee"
            android:singleLine="true" />

        <TextView
            android:id="@+id/artist_and_album"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:textStyle="normal"
            android:textColor="#ffa9a9a9"
            android:ellipsize="marquee"
            android:singleLine="true" />

    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/player_controller"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/position_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingRight="6dip" 
            android:textSize="12sp"
            android:textColor="#ffbbbbbb"
            android:gravity="right" />

        <SeekBar
            android:id="@+id/seek_bar"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:paddingLeft="9dip"
            android:paddingRight="9dip"
            android:progress="0"
            android:progressDrawable="@drawable/player_progress_drawable"
            android:thumb="@drawable/player_progress_thumb"
            android:thumbOffset="9dip"
            android:maxHeight="2dip"
            android:minHeight="2dip" />

        <TextView
            android:id="@+id/duration_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="6dip"
            android:textSize="12sp"
            android:textColor="#ffbbbbbb"
            android:gravity="left" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/shuffle_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:paddingBottom="20dip"
            android:paddingLeft="12dip"
            android:paddingRight="10dip"
            android:paddingTop="8dip"
            android:background="@null"
            android:src="@drawable/btn_player_shuffle_normal" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_toLeftOf="@+id/repeat_button"
            android:layout_toRightOf="@+id/shuffle_button"
            android:paddingLeft="10dip"
            android:paddingRight="10dip"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <net.sourceforge.servestream.button.RepeatingImageButton
                android:id="@+id/previous_button"
                android:background="@null"
                android:paddingBottom="20dip"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/btn_player_prev" />

            <ImageButton
                android:id="@+id/play_pause_button"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingBottom="20dip"
                android:background="@null"
                android:src="@drawable/btn_player_play" />

            <net.sourceforge.servestream.button.RepeatingImageButton
                android:id="@+id/next_button"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingBottom="20dip"
                android:background="@null"
                android:src="@drawable/btn_player_next" />
        </LinearLayout>

        <ImageButton
            android:id="@+id/repeat_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:paddingBottom="20dip"
            android:paddingLeft="10dip"
            android:paddingRight="12dip"
            android:paddingTop="8dip"
            android:background="@null"
            android:src="@drawable/btn_player_repeat_normal" />

    </RelativeLayout>

</LinearLayout>

I also included the following lines in my activity:

    mTrackName = (TextView) findViewById(R.id.trackname);
    mTrackName.setSelected(true);
    mArtistAndAlbumName = (TextView) findViewById(R.id.artist_and_album);
    mArtistAndAlbumName.setSelected(true);

It appears the original layout with the nested RelativeLayout was preventing the text from scrolling.




回答2:


Separate class file:

public class TextScrolling extends TextView {

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

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

    public TextScrolling(Context context) {
        super(context);
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if(focused)
            super.onWindowFocusChanged(focused);
    }


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

}

And in your xml file do this:

<yourpakage name.aboveclassname

    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:textStyle="bold|italic"
    android:id="@+id/scrolltext"
    android:padding="5dip" 
    android:textSize="15dp"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />

Surely this will work..Try it out..




回答3:


Here is my marquee code:

<TextView
        android:id="@+id/scanCode"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:lines="1"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#ffffff" />

Also set in code:

    scanCode.setEnabled(true);
    scanCode.setSelected(true);



回答4:


Set focusable(true) and in code call requestFocus(); If your layout contains other view with focusable(true), you should make sure your textview has focused




回答5:


try setSelected(true) after settext




回答6:


Set in code

yourTexView.setMovementMethod(new ScrollingMovementMethod());


来源:https://stackoverflow.com/questions/13983871/marquee-textview-not-working-in-android-layout

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