閱讀585 返回首頁    go 阿裏雲 go 技術社區[雲棲]


API Demos 2.3 學習筆記 (6)-- Text->Marquee

更多精彩內容,請點擊閱讀:《API Demos 2.3 學習筆記》


在TextView及其子類控件中,當文本內容太長,超過控件長度時,默認情況下,無法完全顯示文本內容。此時,通過在xml布局文件中設置控件的android:ellipsize屬性,可以將無法顯示的部分用省略號表示,並放在文本的起始,中間或者結束位置;還可以跑馬燈的方式來顯示文本(即文本控件獲得焦點時,文本會進行滾動顯示)。具體設置方法如下所示:
1、默認不處理
android:singleLine="true"
android:ellipsize="none"

2、省略號放在起始
android:singleLine="true"
android:ellipsize="start"

3、省略號放在中間
android:singleLine="true"
android:ellipsize="middle"

4、省略號放在結束
android:singleLine="true"
android:ellipsize="end"

5、跑馬燈效果
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"

注:1、android:singleLine="true"表示單行顯示。
2、在設置跑馬燈效果時候,最好加上android:focusable="true"和android:focusableInTouchMode="true",分別表示可以獲得焦點,和在觸摸模式下可以獲得焦點。
3、android:marqueeRepeatLimit表示跑馬燈效果重複顯示的次數,隻能取值marquee_forever和正整數。取值marquee_forever時,表示跑馬燈效果一直重複顯示。


下麵我們進行實例代碼解析:

res-value-string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="marquee_default">This use the default marquee animation limit of 3</string>
    <string name="marquee_once">This will run the marquee animation once</string>
    <string name="marquee_forever">This will run the marquee animation forever</string>
</resources>

res-layout-marquee.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:andro
    android:orientation="vertical"
    android:layout_width="match_parent" 
    android:layout_height="match_parent">
    
    <!-- 默認跑馬燈效果 -->
    <Button
        android:layout_width="150dip" 
        android:layout_height="wrap_content"
        android:text="@string/marquee_default"
        android:singleLine="true"  
        android:ellipsize="marquee"/> 
    
    <!-- 跑馬燈效果,重複播放一次 -->
    <Button
        android:layout_width="150dip" 
        android:layout_height="wrap_content"
        android:text="@string/marquee_once"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="1"/>

     <!-- 跑馬燈效果,一直重複播放 -->       
    <Button
        android:layout_width="150dip" 
        android:layout_height="wrap_content"
        android:text="@string/marquee_forever"      
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"/>  
           
</LinearLayout>

src-com.example.android.apis.text-Marquee.java
package com.example.android.apis.text;

import com.example.android.apis.R;

import android.app.Activity;
import android.os.Bundle;

public class Marquee extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //將marquee布局文件渲染出一個View對象,並作為Activity的默認View
        setContentView(R.layout.marquee);
    }
}

效果預覽:

                            

最後更新:2017-04-02 06:51:58

  上一篇:go Android 2.3 Dev Guide (55)-- Android Supported Media Formats
  下一篇:go API Demos 2.3 學習筆記 (8)-- Views-&gt;ImageButton