Marquee TextView无法在Android Layout中使用

时间:2012-12-21 03:24:25

标签: android layout textview marquee

我正在尝试使用@ + id / trackname和@ + id / artist_and_album来使用以下布局中的选取框设置,但它们不会滚动。我也尝试在实际的Activity中使用setSelected(true)和setFocusable(true),但是没有用。这是布局的图片:

enter image description here

和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>

6 个答案:

答案 0 :(得分:3)

我将布局修改为:

<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>

我的活动中还包括以下几行:

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

看起来嵌套RelativeLayout的原始布局阻止了文本滚动。

答案 1 :(得分:1)

单独的类文件:

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

}

在你的xml文件中执行以下操作:

<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" />

当然这会奏效。试试吧......

答案 2 :(得分:0)

这是我的字幕代码:

<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" />

也在代码中设置:

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

答案 3 :(得分:0)

设置focusable(true)并在代码调用requestFocus()中; 如果您的布局包含其他具有可聚焦(true)的视图,则应确保您的textview具有焦点

答案 4 :(得分:0)

试 的setSelected(真) 在settext之后

答案 5 :(得分:0)

设置代码

yourTexView.setMovementMethod(new ScrollingMovementMethod());