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

實(shí)例8 文字顏色的設(shè)置

【實(shí)例描述】

在本小節(jié)中實(shí)現(xiàn)了一個(gè)TextView文字顯示的小程序,其可以在屏幕上顯示文字,并且可以為其設(shè)置不同的背景色和字體顏色。本實(shí)例的運(yùn)行效果圖,如圖2-8所示。

圖2-8 文字顏色設(shè)置

提示:在本程序開(kāi)始運(yùn)行時(shí),在界面中顯示如圖2-8所示的提示文本信息。

【實(shí)現(xiàn)過(guò)程】

本程序通過(guò)對(duì)TextView顏色設(shè)置的應(yīng)用,實(shí)現(xiàn)了更改TextView背景色和字體顏色的功能。

【代碼解析】

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

代碼位置:見(jiàn)隨書光盤中源代碼/第2章/Sample2_8/ 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            android:text="@+id/TextView01"
    8            android:id="@+id/TextView01"
    9            android:layout_width="wrap_content"
    10           android:layout_height="wrap_content">
    11       </TextView>                                     <!--TextView控件-->
    12       <TextView
    13           android:text="@+id/TextView02"
    14           android:id="@+id/TextView02"
    15           android:layout_width="wrap_content"
    16           android:layout_height="wrap_content">
    17       </TextView>                                     <!--TextView控件-->
    18  </LinearLayout>                                     <!--LinearLayout-->

上面已經(jīng)介紹了本程序的主界面main.xml的開(kāi)發(fā),接下來(lái)向讀者介紹本程序具體功能的實(shí)現(xiàn),代碼如下。

代碼位置:見(jiàn)隨書光盤中源代碼/第2章/Sample2_6/src/com/bn/ex2h目錄下的Sample2_8_Activity.class。

    1   package com.bn. ex2h;                                   //聲明包
    2   ……//該處省略了部分類的導(dǎo)入,讀者可自行查看隨書光盤中源代碼
    3   import android.widget.TextView;                         //導(dǎo)入相關(guān)類
    4   public class Sample2_8_Activity extends Activity {      //繼承自Activity類
    5      @Override
    6      public void onCreate(Bundle savedInstanceState){    //重寫的方法
    7          super.onCreate(savedInstanceState);               //調(diào)用父類
    8          setContentView(R.layout.main);                    //跳轉(zhuǎn)到主界面
    9          TextView tv01=(TextView)this.findViewById(R.id.TextView01);
    10         tv01.setText("設(shè)置文字背景色");                     //設(shè)置TextView的文本
    11         Resources resources=getBaseContext().getResources();  //得到資源的引用
    12         Drawable Hdrawable=resources.getDrawable(R.color.white);
                                                                //得到圖片的引用
    13         tv01.setBackgroundDrawable(Hdrawable);            //設(shè)置圖片為背景
    14         TextView tv02=(TextView)this.findViewById(R.id.TextView02);//得到引用
    15         tv02.setText("設(shè)置文字顏色");                       //設(shè)置TextView的文本
    16         tv02.setTextColor(Color.RED);                     //設(shè)置顏色
    17     }}

提示:上面主要介紹了TextView在實(shí)際中的應(yīng)用,可以在屏幕上顯示不同背景和不同顏色的文字。

主站蜘蛛池模板: 西藏| 万安县| 定襄县| 边坝县| 长寿区| 商水县| 浦城县| 苏尼特右旗| 汝阳县| 横峰县| 衡东县| 曲阳县| 六枝特区| 南川市| 宿州市| 嵩明县| 东乡族自治县| 泾川县| 达拉特旗| 泰和县| 河源市| 涞源县| 逊克县| 临泽县| 忻州市| 天全县| 乌鲁木齐市| 思茅市| 吉林省| 广饶县| 那坡县| 积石山| 永济市| 阜南县| 金华市| 安远县| 凤凰县| 武夷山市| 苍梧县| 赤壁市| 浠水县|