Android的 - 问题与TextView的自动滚动 [英] android - issue with auto scrolling in TextView

查看:179
本文介绍了Android的 - 问题与TextView的自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动它作为一个音乐播放器。当它启动时,的MediaPlayer 对象被初始化并启动。在布局,我有 TextViews 来显示艺术家和标题。这些值(从服务器检索)可能会很长,所以我说

 的android:ellipsize =金字招牌
   机器人:marqueeRepeatLimit =marquee_forever
   机器人:scrollHorizo​​ntally =真

,使文本滚动。有一个奇怪的问题:当我preSS的暂停按钮,在 TextViews 是滚动的,但同时,正在播放的音乐,他们不滚动。有任何想法吗?

P.S。我更新搜索栏使用的Runnable ,可以在问题与此有关的每500ms的?

下面是我的布局:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#000000
机器人:重力=center_vertical
工具:上下文=MainActivity。><的LinearLayout
    机器人:ID =@ + ID / main_header
    机器人:layout_width =match_parent
    机器人:layout_height =50dp
    机器人:layout_alignParentTop =真
    机器人:方向=横向>
< / LinearLayout中>< ImageView的
    机器人:ID =@ + ID / imgCoverArt
    机器人:layout_width =150dp
    机器人:layout_height =150dp
    机器人:layout_below =@ + ID / main_header
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginBottom =15dp
    机器人:layout_marginTop =20dp
    机器人:SRC =@绘制/ cover_default/>    <搜索栏
    机器人:ID =@ + ID / seekBarMain
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / imgCoverArt
    机器人:layout_marginLeft =20dp
    机器人:layout_marginRight =20dp
    机器人:layout_marginTop =10dp
    机器人:paddingLeft =9dp
    机器人:paddingRight =9dp
    机器人:progressDrawable =@绘制/ PB
    机器人:拇指=@绘制/ pbhead/><的TextView
    机器人:ID =@ + ID / tvArtist
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / seekBarMain
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginTop =35dp
    机器人:ellipsize =金字招牌
    机器人:marqueeRepeatLimit =marquee_forever
    机器人:scrollHorizo​​ntally =真
    机器人:单线=真
    机器人:文字=大文本
    机器人:textAppearance =机器人:ATTR / textAppearanceLarge
    机器人:文字颜色=#FFFFFF/><的TextView
    机器人:ID =@ + ID / tvTitle
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_below =@ + ID / tvArtist
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginLeft =5DP
    机器人:layout_marginRight =5DP
    机器人:ellipsize =金字招牌
    机器人:marqueeRepeatLimit =marquee_forever
    机器人:scrollHorizo​​ntally =真
    机器人:单线=真
    机器人:文字=中文字
    机器人:textAppearance =机器人:ATTR / textAppearanceMedium
    机器人:文字颜色=#FFFFFF/><的TextView
    机器人:ID =@ + ID / tvTimeElapsed
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID / seekBarMain
    机器人:layout_alignLeft =@ + ID / seekBarMain
    机器人:layout_marginBottom = - 10dp
    机器人:文字=02:54
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#FFFFFF/><的TextView
    机器人:ID =@ + ID / tvTotalTime
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID / seekBarMain
    机器人:layout_alignRight =@ + ID / seekBarMain
    机器人:layout_marginBottom = - 10dp
    机器人:文字=05:45
    机器人:textAppearance =机器人:ATTR / textAppearanceSmall
    机器人:文字颜色=#FFFFFF/><的LinearLayout
    机器人:ID =@ + ID / LinearLayout1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_marginBottom =10dp
    机器人:重力=CENTER_HORIZONTAL
    机器人:方向=横向>    <按钮
        机器人:ID =@ + ID / BTN previous
        机器人:layout_width =100dp
        机器人:layout_height =40dp
        机器人:layout_gravity =center_vertical
        机器人:layout_marginRight =6DP
        机器人:背景=@绘制/ prev_btn_state/>    <按钮
        机器人:ID =@ + ID / btnPlay
        机器人:layout_width =80dp
        机器人:layout_height =80dp
        机器人:layout_gravity =center_vertical
        机器人:背景=@绘制/ button_play/>    <按钮
        机器人:ID =@ + ID / btnNext
        机器人:layout_width =100dp
        机器人:layout_height =40dp
        机器人:layout_gravity =center_vertical
        机器人:layout_marginLeft =3DP
        机器人:背景=@绘制/ next_btn_state/>
< / LinearLayout中>< / RelativeLayout的>


解决方案

 <的TextView
        机器人:文字=自动滚动单行文本视图如果文本太长而不能在窗口小部件
        机器人:单线=真
        机器人:ellipsize =金字招牌
        机器人:marqueeRepeatLimit =marquee_forever
        机器人:可聚焦=真
        机器人:focusableInTouchMode =真
        机器人:scrollHorizo​​ntally =真
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>

添加此您TextView的,

 机器人:可聚焦=真
            机器人:focusableInTouchMode =真

是需要自动滚动。

更多的例子是那里的GitHub。

https://github.com/FireZenk/FireZenk-s-TickerText

<一个href=\"https://github.com/kaeppler/ignition/blob/master/ignition-core/ignition-core-lib/src/com/github/ignition/core/widgets/ScrollingTextView.java\" rel=\"nofollow\">https://github.com/kaeppler/ignition/blob/master/ignition-core/ignition-core-lib/src/com/github/ignition/core/widgets/ScrollingTextView.java

希望这会帮助你。

I have an Activity which serves as a music player. When it starts, a MediaPlayer object gets initialized and started. In the layout, I have TextViews to display the artist and the title. Those values (retrieved from the server) can be long so I added

   android:ellipsize="marquee"
   android:marqueeRepeatLimit="marquee_forever"
   android:scrollHorizontally="true"

to make the text scroll. There is a strange issue: when I press the "pause" button, the TextViews are scrolling, but while the music is playing they don't scroll. Any ideas?

P.S. I update the SeekBar every 500ms using a Runnable, could the issue be related to this?

Here's my layout:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_vertical"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/main_header"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentTop="true"
    android:orientation="horizontal" >
</LinearLayout>

<ImageView
    android:id="@+id/imgCoverArt"
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:layout_below="@+id/main_header"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="15dp"
    android:layout_marginTop="20dp"
    android:src="@drawable/cover_default" />

    <SeekBar
    android:id="@+id/seekBarMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imgCoverArt"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="9dp"
    android:paddingRight="9dp"
    android:progressDrawable="@drawable/pb"
    android:thumb="@drawable/pbhead" />

<TextView
    android:id="@+id/tvArtist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/seekBarMain"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvArtist"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:ellipsize="marquee"
    android:marqueeRepeatLimit="marquee_forever"
    android:scrollHorizontally="true"
    android:singleLine="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF" />



<TextView
    android:id="@+id/tvTimeElapsed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBarMain"
    android:layout_alignLeft="@+id/seekBarMain"
    android:layout_marginBottom="-10dp"
    android:text="02:54"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<TextView
    android:id="@+id/tvTotalTime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/seekBarMain"
    android:layout_alignRight="@+id/seekBarMain"
    android:layout_marginBottom="-10dp"
    android:text="05:45"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#FFFFFF" />

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnPrevious"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="6dp"
        android:background="@drawable/prev_btn_state" />

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center_vertical"
        android:background="@drawable/button_play" />

    <Button
        android:id="@+id/btnNext"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="3dp"
        android:background="@drawable/next_btn_state" />
</LinearLayout>

</RelativeLayout>

解决方案

<TextView
        android:text="Single-line text view that scrolls automatically if the text is too long to fit in the widget" 
        android:singleLine="true" 
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true" 
        android:scrollHorizontally="true"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>

add this your TextView,

            android:focusable="true"
            android:focusableInTouchMode="true" 

is needed to scroll automatically.

More example are there in GitHub.

https://github.com/FireZenk/FireZenk-s-TickerText

https://github.com/kaeppler/ignition/blob/master/ignition-core/ignition-core-lib/src/com/github/ignition/core/widgets/ScrollingTextView.java

Hope this will help you.

这篇关于Android的 - 问题与TextView的自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆