2

我已经尝试了所有,我能想到的让这个选框效果起作用。这是我的xml:

<TextView android:id="@+id/curPlaying"
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:ellipsize="marquee"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:marqueeRepeatLimit ="marquee_forever"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    android:textColor="#83A602"
    android:textSize="20dp"
    android:text="Nothing Loaded" />

我将它设置为在代码中选择。我认为它可能不起作用的唯一原因是代码以相对频繁的间隔修改了文本。

帮助?

4

4 回答 4

1

嗨,请尝试下面的代码,它对我来说工作正常......

<TextView
            android:id="@+id/mywidget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:lines="1"
            android:marqueeRepeatLimit="marquee_forever"
            android:scrollHorizontally="true"
            android:text="Simple application that shows how to use marquee, with a long text"
            android:textColor="#ff4500" />

TextView tv = (TextView) this.findViewById(R.id.mywidget);
        tv.setSelected(true); // Set focus to the textview

它正在我的三星 Galaxy ACE 手机上运行。

于 2012-05-05T04:52:42.740 回答
0

您可以查看Android 1.1R1 中 TextView 的 Marquee 功能,它解释了 marquee 在工作中可能出现问题的某些方式。ScrollTextView - 为 Android 滚动 TextView可能是一个不错的选择。

于 2012-05-05T04:48:34.547 回答
0

如果TextView填充整个宽度,您需要添加这些代码行

<TextView
    .
    .
    android:singleLine="true"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    />

也不要忘记设置选择TexrtView如下

textview.setSelected(true);

如果TextView不填充宽度,唯一的就是调用这个方法并传递TextView给它。

    fun addMarquee(textView: TextView) {
    textView.viewTreeObserver.addOnGlobalLayoutListener(object :
        ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            val pixels = textView.measuredWidth - 1
            val params = textView.layoutParams
            params.width = pixels
            textView.layoutParams = params
            textView.isSelected = true
            textView.ellipsize = TextUtils.TruncateAt.MARQUEE
            textView.isSingleLine = true
            textView.marqueeRepeatLimit = -1
            textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })
}

注意:此方法仅在 textView 宽度为wrap_content.

于 2021-04-09T15:12:49.590 回答
0

我发帖是因为没有可接受的答案......并帮助未来访问 Stack Overflow 寻求答案的访客。

  public class AlwaysMarqueeTextView extends TextView {

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

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

  public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  super(context, attrs, defStyleAttr, defStyleRes);
  }

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

活动.xml

<ivar.kov.util.textViewUtil.AlwaysMarqueeTextView
  android:id="@+id/artist_tv2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:ellipsize="marquee"
  android:focusable="true"
  android:focusableInTouchMode="true"
  android:marqueeRepeatLimit="marquee_forever"
  android:scrollHorizontally="true"
  android:singleLine="true"
  android:text="@string/appbar_scrolling_view_behavior"/>
于 2017-01-28T11:11:10.030 回答