Ryan's Java world!

          something about Java and opensource!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            51 Posts :: 25 Stories :: 59 Comments :: 0 Trackbacks
          在Android中要顯示跑馬燈是比較容易的,只要設(shè)置2個(gè)屬性就可以了:
          android:singleLine="true"
          android:ellipsize="marquee"

          但 是要顯示跑馬燈該View必需是可以取得焦點(diǎn)的,只有在取得焦點(diǎn)的情況下跑馬燈才會(huì)出現(xiàn).
          如果是組合View的情況下就有問題了, 如下一個(gè)組合View:
          <?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個(gè)TextView組合為一個(gè)View,由于設(shè)置了LinearLayout為focusable而TextView就沒法取得焦點(diǎn)了,這樣 這個(gè)TextView的跑馬燈效果就顯示不出來,就算你也設(shè)置TextView的 android:focusable="true" 也是沒用的. 這個(gè)時(shí)候就要使用addStatesFromChildren 這個(gè)屬性了,在LinearLayout中設(shè)置這個(gè)屬性,然后設(shè)置TextView的focusable="true" 就可以了.關(guān)于 addStatesFromChildren的說明:

          Sets whether this ViewGroup's drawable states also include its children's drawable states.

          可以正常顯示的代碼:
          <?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>

          posted on 2010-05-31 19:07 冰雨 閱讀(619) 評(píng)論(2)  編輯  收藏 所屬分類: Java

          Feedback

          # re: addStatesFromChildren 和跑馬燈 2010-06-12 15:27 jue
          很厲害,你怎么想到的?如果是我根本想不到。  回復(fù)  更多評(píng)論
            

          # re: addStatesFromChildren 和跑馬燈 2010-06-12 15:42 jue
          如果是其他的可以獲得焦點(diǎn)的控件怎么辦?比如說一個(gè)EditText和一個(gè)跑馬燈?  回復(fù)  更多評(píng)論
            


          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          JSF中文技術(shù)文摘
          主站蜘蛛池模板: 昌图县| 石阡县| 安塞县| 永胜县| 手游| 三门县| 南召县| 西安市| 罗田县| 颍上县| 石台县| 菏泽市| 上虞市| 北京市| 东乡族自治县| 阿荣旗| 酉阳| 孟津县| 西乌珠穆沁旗| 博乐市| 桃江县| 临夏县| 辽宁省| 涟源市| 会泽县| 高碑店市| 中超| 海门市| 乐业县| 威信县| 嘉黎县| 长白| 环江| 西丰县| 乐业县| 东安县| 百色市| 松江区| 海兴县| 正宁县| 正阳县|