【问题标题】:Android: Auto-scrolling text in TextViewAndroid:在 TextView 中自动滚动文本
【发布时间】:2012-06-25 05:50:26
【问题描述】:

我创建了一系列固定宽度为 100 像素的选项卡。选项卡包含一个图像,其下方有一些文本。如果文本太长而无法容纳,我希望它自动滚动。我只想要一条线。我尝试了以下但没有奏效。我使用的是 Android 2.3:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="3dp"
    android:layout_marginTop="3dp"
    android:background="#ff737373"
    android:gravity="center"
    android:minWidth="64dp"
    android:orientation="vertical"
    android:padding="3dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:tag="tabImage" >
    </ImageView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:maxWidth="100px"
        android:tag="tabCaption"
        android:textColor="#ffd9d9d9"
        android:textSize="16sp" />

</LinearLayout>

知道为什么这不起作用吗?我从另一个帖子中找到了解决方案,用户表示它有效。

【问题讨论】:

    标签: android scroll textview


    【解决方案1】:

    如果你想在文本上选框,你必须在你的活动中添加

      TextView tv=(TextView)findViewById(R.id.textview1);
      tv.setSelected(true);
    

    【讨论】:

    • 发布此内容后,我进行了更多搜索,也遇到了这个问题。还是非常感谢!
    【解决方案2】:
    【解决方案3】:
    <TextView
    
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:gravity="center"
            android:ellipsize="marquee"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:marqueeRepeatLimit="marquee_forever"
            android:text="Auto text scroller" 
            android:textSize="50sp"
            />
    

    【讨论】:

      【解决方案4】:

      请注意,如果文本的长度小于为其定义的边界的宽度,它将不会滚动。在这种情况下,您可能希望使用自身扩展文本,例如:

      -。 . . . .

      String charsInBreak = "   ";
       while (bounds.width() < this.m_width)
       {
          m_text = (m_text + charsInBreak + m_text);
          paint.getTextBounds(m_text, 0, m_text.length(), bounds);
       } 
      

      如果你想让你的文字永远选框:

      m_textView.setMarqueeRepeatLimit(-1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-09
        • 2018-07-20
        • 1970-01-01
        • 2011-03-31
        • 2011-07-25
        • 1970-01-01
        • 2011-11-18
        相关资源
        最近更新 更多