`
zhifeiji512
  • 浏览: 115984 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android:TextView 自动滚动(跑马灯)

阅读更多

 

参考文章:

  1. Android2.2 API 中文文档系列(1) —— TextView
  2. [Android1.5]TextView跑马灯效果
  3. addStatesFromChildren 和跑马灯
  4. TextView总是显示跑马灯效果

思想介绍:

TextView自带跑马灯的效果,需要设置一下TextView的属性,把显示模式改为“跑马灯”。设置滚动次数。
这些都还不够,因为TextView的跑马灯跑起来,需要此textView得到焦点。所以要想个办法解决一下,让他总是滚动。

简要步骤:

在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。

示例1:
Java语言
AlwaysMarqueeTextView 类

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 defStyle) {
      super(context, attrs, defStyle);
}

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

 

在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。

<com.examples.AlwaysMarqueeTextView
	android:id=“@+id/AMTV1″ 
	android:layout_width=“fill_parent”
	android:layout_height=“wrap_content” 
	android:lines=“1″
	android:focusable=“true” 
	android:focusableInTouchMode=“true”
	android:scrollHorizontally=“true” 
	android:marqueeRepeatLimit=“marquee_forever”
	android:ellipsize=“marquee” 
	android:background=“@android:color/transparent” />
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。

focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。

示例2:
组合View的问题:

XML语言: 组合View
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:gravity="center_vertical"
	android:background="@drawable/f_background" 
	android:layout_width="fill_parent"
	android:focusable="true" android:layout_height="50px">
	<TextView android:id="@+id/info_text" 
		android:focusable="true"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:text="test marquee ... "
		android:textColor="@color/black"
		android:singleLine="true" 
		android:ellipsize="marquee"
		android:marqueeRepeatLimit="3"
		android:textSize="18sp" />
	<TextView android:id="@+id/date_texfocusablet" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:layout_gravity="bottom"
		android:textColor="@color/gray" 
		android:text="2011/04/01"
		android:textSize="12sp"/>
</LinearLayout >

 

    上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的 android:focusable= "true" 也是 没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,然后设置TextView的focusable= "true" 就可以了.关于 addStatesFromChildren的说明:
Sets whether this ViewGroup's drawable states also include its children's drawable states.

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics