AndroidGUI15:Style常用技巧

1.     如下圖,創建一個 styles.xml 文件

編輯其內容,使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< resources >

         < style name = "small_font" >

                   < item name = "android:textSize" > 10px </ item >

         </ style >

        

         < style name = "big_font" >

                   < item name = "android:textSize" > 20px </ item >

         </ style >

</ resources >

 

2.     修改 main.xml 使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

    android:orientation = "vertical"

    android:layout_width = "fill_parent"

    android:layout_height = "fill_parent"

    >

         < TextView

style = "@style/small_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

    < TextView

                   style = "@style/big_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

</ LinearLayout >

 

運行結果:


 

3.     Style 還支持繼承。比如我們修改前面的 styles.xml ,使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< resources >

         < style name = "small_font" >

                   < item name = "android:textSize" > 10px </ item >

         </ style >

        

         < style name = "big_font" >

                   < item name = "android:textSize" > 20px </ item >

         </ style >

        

         < style name = "red_small_font" parent = "small_font" >

                   < item name = "android:textColor" > #F00 </ item >

         </ style >

        

         < style name = "yellow_big_font" parent = "big_font" >

                   < item name = "android:textColor" > #FF0 </ item >

         </ style >

        

         < style name = "bold_red_small_font" parent = "red_small_font" >

                   < item name = "android:textStyle" > bold </ item >

         </ style >

        

         < style name = "italic_yellow_big_font" parent = "yellow_big_font" >

                   < item name = "android:textSize" > 28px </ item >

                   < item name = "android:textStyle" > italic </ item >

         </ style >     

</ resources >

在上面的 italic_yellow_big_font 中,我們把字體的大小改成了 28px

 

4.     main.xml 修改如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"

    android:orientation = "vertical"

    android:layout_width = "fill_parent"

    android:layout_height = "fill_parent"

    >

         < TextView

                   style = "@style/small_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

    < TextView

                   style = "@style/big_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

         < TextView

                   style = "@style/red_small_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

    < TextView

                   style = "@style/yellow_big_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

         < TextView

                   style = "@style/bold_red_small_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />

   

    < TextView

                   style = "@style/italic_yellow_big_font"

             android:layout_width = "fill_parent"

             android:layout_height = "wrap_content"

             android:text = "@string/hello"

    />        

</ LinearLayout >

 

運行結果如下:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章