TextView跑马灯效果与addStatesFromChildren属性关系

在Android中要显示跑马灯是比较容易的,只要设置2个属性就可以了:
android:singleLine="true"
android:ellipsize="marquee"

但 是要显示跑马灯该View必需是可以取得焦点的,只有在取得焦点的情况下跑马灯才会出现.
如果是组合View的情况下就有问题了, 如下一个组合View:

<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <?  xml version="1.0" encoding="utf-8"  ?> 
<  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_text" 
      android:layout_width
  ="fill_parent" 
      android:layout_height
  ="wrap_content" 
      android:layout_gravity
  ="bottom" 
      android:textColor
  ="@color/gray" 
      android:text
  ="2010/05/28" 
      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 drawablestates also include its children's drawablestates.

可以正常显示的代码:

<!-- <br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> <?  xml version="1.0" encoding="utf-8"  ?> 
<  LinearLayout
  
  xmlns:android  ="http://schemas.android.com/apk/res/android" 
  android:orientation
  ="vertical" 
  android:gravity
  ="center_vertical" 
  android:background
  ="@drawable/zixun_background" 
  android:layout_width
  ="fill_parent" 
  android:addStatesFromChildren
  ="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
  =" " 
      android:textColor
  ="@color/black" 
      android:singleLine
  ="true" 
      android:ellipsize
  ="marquee" 
      android:marqueeRepeatLimit
  ="3" 
      android:textSize
  ="18sp" 
  
  /> 
    <  TextView 
      
  android:id  ="@+id/date_text" 
      android:layout_width
  ="fill_parent" 
      android:layout_height
  ="wrap_content" 
      android:layout_gravity
  ="bottom" 
      android:textColor
  ="@color/gray" 
      android:text
  ="2010/05/28" 
      android:textSize
  ="12sp" 
  
  /> 
</  LinearLayout  > 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值