Android 多textview跑马灯

问题描述 投票:0回答:3

有很多文本视图,可能有很长的文本,所以已经完成了

android:marqueeRepeatLimit="marquee_forever"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:scrollHorizontally="true"
    android:focusable="true"
    android:focusableInTouchMode="true" 

对于第一个文本视图,它工作正常,但对于其他文本视图,它不起作用,如果有人在活动中为其他文本视图完成了它,那么请帮助我。

谢谢你。

android textview
3个回答
7
投票

使用 TextView 扩展您的类并覆盖以下方法非常容易

package com.az.app;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class ScrollingTextView extends TextView {

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

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

    public ScrollingTextView(Context context) {
        super(context);
    }

    // ===========================================================
    // Constants
    // ===========================================================

    // ===========================================================
    // Fields
    // ===========================================================

    // ===========================================================
    // Constructors
    // ===========================================================

    // ===========================================================
    // Getter & Setter
    // ===========================================================

    // ===========================================================
    // Methods for/from SuperClass/Interfaces
    // ===========================================================
    @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;
    }
    // ===========================================================
    // Methods
    // ===========================================================

    // ===========================================================
    // Inner and Anonymous Classes
    // ===========================================================


}

并在您的 XML 中执行此操作

<com.az.app.ScrollingTextView
                        android:id="@+id/TextView02"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/ImageView01"
                        android:ellipsize="marquee"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:scrollHorizontally="true"
                        android:singleLine="true"
                        android:text="This is a really very very very very very long text "
                        android:textAppearance="?android:attr/textAppearanceSmall" />
                </RelativeLayout>
<com.az.app.ScrollingTextView
                        android:id="@+id/TextView03"
                        android:layout_width="140dp"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_toRightOf="@+id/ImageView01"
                        android:ellipsize="marquee"
                        android:focusable="true"
                        android:focusableInTouchMode="true"
                        android:marqueeRepeatLimit="marquee_forever"
                        android:scrollHorizontally="true"
                        android:singleLine="true"
                        android:text="This is a really very very very very very long text "
                        android:textAppearance="?android:attr/textAppearanceSmall" />
                </RelativeLayout>

现在所有文本视图都将滚动。

更新:
请注意,如果没有

android:singleLine="true"
,它就不起作用。 与
android:maxLines="1"
一起使用,尽管我们知道它已被弃用。


0
投票

@Sathish 是的,你可以,我已经做到了。以下是我在 xamarin.android 中的两个选框文本视图的代码

xml

<TextView
            android:text=""
            android:singleLine="true"
            android:background="#cc2900"
            android:ellipsize="marquee"
            android:textColor="#FFFFFF"
            android:textSize="15dp"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:layout_marginLeft="1dp"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:id="@+id/TextView03"
            android:padding="5dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
      <TextView
            android:text=""
            android:singleLine="true"
            android:background="#cc2900"
            android:ellipsize="marquee"
            android:textColor="#FFFFFF"
            android:textSize="15dp"
            android:paddingTop="4dp"
            android:paddingBottom="4dp"
            android:layout_marginLeft="1dp"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:id="@+id/txtinternational"
            android:padding="5dip"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

在您的活动中,您只需将焦点设置到所有文本视图即可:

TextView shorts = FindViewById<TextView>(Resource.Id.TextView03);
            shorts.Selected = true;

TextView internationalshorts = FindViewById<TextView>(Resource.Id.txtinternational);
        internationalshorts.Selected = true;

0
投票

2023 年 12 月,这仍然是一个问题。这是 kotlin 中的 TextView 子类,它的属性也是以编程方式定义的,因此不需要在 xml 中执行任何操作。

class MarqueeTextView: TextView {
    ... enter constructors ...
    init {
        ellipsize = TruncateAt.MARQUEE
        marqueeRepeatLimit = -1
        setSingleLine()
        isFocusable = true
        isFocusableInTouchMode = true
        setHorizontallyScrolling(true)
    }

    override fun isFocused() = true
}
© www.soinside.com 2019 - 2024. All rights reserved.