---
title: "addStatesFromChildren 和跑马灯"
author: "icess"
date: "2010-05-31T19:03:22Z"
tags: []
categories: ["移动开发"]
slug: "addstatesfromchildren"
draft: false
lastmod: "2010-05-31T19:03:22Z"
id: 680257
url: "https://jsf.iteye.com/blog/680257"
---

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

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

&lt;!-- &lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt;? xml version="1.0" encoding="utf-8" ?&gt;  
&lt; 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" &gt;  
   &lt; TextView   
       android:id ="@+id/info\_text"  
      android:focusable ="true"  
      android:layout\_width ="fill\_parent"  
      android:layout\_height ="wrap\_content"  
      android:text ="test marquee ![](http://www.blogjava.net/Images/dot.gif)![](http://www.blogjava.net/Images/dot.gif)![](http://www.blogjava.net/Images/dot.gif)![](http://www.blogjava.net/Images/dot.gif)![](http://www.blogjava.net/Images/dot.gif)![](http://www.blogjava.net/Images/dot.gif) .. "  
      android:textColor ="@color/black"  
      android:singleLine ="true"  
      android:ellipsize ="marquee"  
      android:marqueeRepeatLimit ="3"  
      android:textSize ="18sp"  
   /&gt;  
   &lt; 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"  
   /&gt;  
&lt;/ LinearLayout &gt;

上面示例中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. 可以正常显示的代码:`

&lt;!-- &lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;&lt;? xml version="1.0" encoding="utf-8" ?&gt;  
&lt; 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" &gt;  
   &lt; 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"  
   /&gt;  
   &lt; 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"  
   /&gt;  
&lt;/ LinearLayout &gt;