官术网_书友最值得收藏!

實例9 使你的文字顯得更獨特

【實例描述】

在本小節中實現了一個使用Style樣式化TextView的小程序,通過Style樣式化的應用,避免了每次都設置TextView文字大小、TextView顏色等。本實例的運行效果圖,如圖2-9所示。

圖2-9 樣式化文本效果圖

提示:在本程序開始運行時,在界面中顯示如圖2-9所示的提示文本信息。

【實現過程】

本程序通過使用Style樣式化TextView,可以實現TextView設置的批量處理。

【代碼解析】

首先為讀者介紹本程序的主界面main.xml的開發,代碼如下。

代碼位置:見隨書光盤中源代碼/第2章/Sample2_9/ res/layout目錄下的main.xml。

    1   <?xml version="1.0" encoding="utf-8"?>
    2   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3      android:orientation="vertical"
    4      android:layout_width="fill_parent"
    5      android:layout_height="fill_parent">             <!--LinearLayout-->
    6        <TextView
    7           style="@style/style01"
    8            android:text=" android實例"
    9            android:id="@+id/TextView01"
    10           android:layout_width="wrap_content"
    11           android:layout_height="wrap_content">
    12       </TextView>                                     <!--TextView控件-->
    13       <TextView
    14          style="@style/style02"
    15           android:text=" android實例"
    16           android:id="@+id/TextView02"
    17           android:layout_width="wrap_content"
    18           android:layout_height="wrap_content">
    19       </TextView>                                <!--TextView控件-->
    20  </LinearLayout>                                 <!--LinearLayout-->

上面已經介紹了本程序的主界面main.xml的開發,接下來介紹本程序關鍵的style.xml文件。

代碼位置:見隨書光盤中源代碼/第2章/Sample2_9/ res/valuest目錄下的sytle.xml。

    1   <?xml version="1.0" encoding="utf-8"?>          <!--版本號和編碼方式-->
    2   <resources>
    3        <style name="style01">
    4            <item name="android:textSize">16sp</item>       <!--設置大小-->
    5            <item name="android:textColor">#FFFFFF</item>  <!--設置顏色-->
    6        </style>
    7        <style name="style02">
    8            <item name="android:textSize">20sp</item>       <!--設置大小-->
    9            <item name="android:textColor">#fd8d8d</item>  <!--設置顏色-->
    10           <item name="android:fromAlpha">0.0</item>  <!--設置起始的透明度-->
    11           <item name="android:toAlpha">0.0</item>    <!--設置最終的透明度-->
    12       </style>
    13  </resources>

上面已經介紹了本程序的main.xml與style.xml的開發,接下來為讀者介紹本程序具體功能的實現,代碼如下。

代碼位置:見隨書光盤中源代碼/第2章/Sample2_6/src/com/bn/ex2i目錄下的Sample2_9_Activity.class。

    1   package com.bn. ex2i;                               //聲明包
    2   import android.app.Activity;                        //導入相關類
    3   import android.os.Bundle;                           //導入相關類
    4   public class Sample2_9_Activity extends Activity {  //繼承自Activity類
    5      /** Called when the activity is first created. */
    6      @Override
    7      public void onCreate(Bundle savedInstanceState){//重寫的方法
    8          super.onCreate(savedInstanceState);           //調用父類
    9          setContentView(R.layout.main);                //跳轉到主界面
    10     }}

提示:上面的代碼主要是跳轉到主界面,所有顯示的工作全部都在main.xml中完成。

主站蜘蛛池模板: 勃利县| 吕梁市| 岗巴县| 阳东县| 申扎县| 毕节市| 青岛市| 广灵县| 东城区| 绥德县| 黄石市| 新源县| 随州市| 南汇区| 福泉市| 神农架林区| 襄汾县| 兴宁市| 兴和县| 商水县| 武山县| 逊克县| 清流县| 敦煌市| 镇赉县| 砀山县| 金门县| 百色市| 旬阳县| 噶尔县| 军事| 南平市| 吉安县| 赤壁市| 周至县| 天水市| 昌宁县| 巴中市| 汕头市| 乐至县| 化州市|