【轉】Pro Android學習筆記(二四):使用者介面和控制(12):Style和Theme

來源:互聯網
上載者:User

標籤:

目錄(?)[-]

  1. 靜態格式
  2. 代碼中設定
  3. Style
  4. Theme
靜態格式

在res/values中設定靜態Style,在資源中設定靜態Style可使用的HTML格式有<i> <u> <b> <sup> <sub> <strike> <big> <small> <monospace>。

<string name="ui_styleText_1"><i>Static</i> style <u>in</u> a <b>TextView</b>. <strike>strike</strike></string>

我們在XML中進行實驗,也順帶看看其他效果的設定。

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:autoLink="web|email" 
    android:text="Please visit www.androidbook.com for more help on using Android."
    android:minLines="3" 
    android:typeface="serif" /> 
<TextView android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/ui_styleText_1"/>

代碼中設定

在代碼中通過Spannable中設定Style。如所示。對於EditText,我們在xml中設定了 android:inputType="text|textAutoCorrect|textAutoComplete|textMultiLine",故中出現紅色底線,表示拼字錯誤。

//TextView需要先指定BufferType,才能通過getText( )擷取spannable對象。
TextView tv = (TextView)findViewById(R.id.ui_style_tv); 
tv.setText("This text is stored in a Spannable", TextView.BufferType.SPANNABLE); 
Spannable spanTv = (Spannable)tv.getText();
  
//通過setSpan(),對text中的某個範圍,本例0~7的字元進行處理 
spanTv.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
spanTv.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC),0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

//EditText可以直接擷取通過getText()擷取Spannable對象 
EditText et = (EditText)findViewById(R.id.ui_style_ed); 
et.setText("Styling the content of an EditText dynamically"); 
Spannable spanEt = (Spannable)et.getText();  
spanEt.setSpan(new BackgroundColorSpan(Color.RED), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanEt.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Style

如果相同的格式用於多個控制項,每個控制項都寫一遍,很麻煩,可以定義成為style,並在控制項中設定style。style在res/values/中定義,如下所示。

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 例子1:定義名字為“MyErrorText”的style, 設定顏色和字型,還順帶設定了width和height,不用每次寫這麼煩 -->
    <style name="MyErrorText"> 
        <item name="android:layout_width">match_parent</item> 
        <item name="android:layout_height">wrap_content</item> 
        <item name="android:textColor">#FF0000</item> 
        <item name="android:typeface">monospace</item> 
    </style>  
    <!-- 例子2:style樹狀階層:style提供一個很方便的階層,可以一層一層地通過“.”進行擴充  -->
    <style name="MyErrorText.Danger"> 
        <item name="android:textStyle">bold</item> 
    </style> 
    <!-- 例子3:對於擴充android內建的style,不能採用“.”,而是用parent。android內建的style可以在sdk\platforms\android-xx\data\res\values\styles.xml 查看 -->
    <style name="MyText" parent="@android:style/TextAppearance.Small">
        <item name="android:textColor">#FF00FF</item> 
    </style>    
</resources>

layout xml檔案的相關內容如下:

    <-- 調用style,和其他屬性不同,前面沒有android:  --> 
    <TextView style="@style/MyErrorText" 
        android:text="Error: No Error here." /> 
    <-- 調用居於階層的style --> 
     <TextView style="@style/MyErrorText.Danger" 
        android:text="Fatal Error: Test...." /> 
     <-- 調用android系統自訂的style--> 
    <TextView style="@android:style/TextAppearance.Holo" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Android Default Style : TextAppearance.Holo" /> 
    <-- 測試style的例子3的效果 --> 
    <TextView style="@style/MyText" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Information: my text" /> 
    <-- 對於系統定義的Theme,我們可以只使用其中的某個屬性  --> 
    <EditText android:id="@+id/ui_style_ed2" 
         android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="?android:textColorSecondary" 
        android:text="@string/ui_styleTest"/>

Theme

在上面的例子中已經提到theme。使用style可以避免在逐個控制項中進行控制項屬性的描述,如果修改可以只在一個地方進行修改,提供了很大的便捷,但是仍需要在每個控制項中進行style指定。如果希望屬效能夠在整個activity或者整個application都實用,可以採用theme。theme和style概念上很相似,在定義屬性時,一樣採用style在res/vaules/的xml檔案中描述。如下面的例子。android系統自訂的theme在sdk\platforms\android-xx\data\res\values\themes.xml中定義。

<style name="MyTheme" parent="@android:style/Theme"> 
    <item name="android:textColor">#666666</item> 
</style> 

Theme在AndroidManifest.xml中設定。

對於activity: 
    <activity ... android:theme="@style/MyTheme" ... /> 
對於application: 
   <application .... android:theme="@style/MyTheme" ... />

相關連結: 我的Android開發相關文章

【轉】Pro Android學習筆記(二四):使用者介面和控制(12):Style和Theme

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.