Android中的style和theme

本文轉自:http://jiayanjujyj.iteye.com/blog/1392541

 

 

最近在做軟件從2.3到4.0的改變的一些工作,其中涉及了一些style和theme相關的東西。上網上查了一些東西,這個一併說說。關於android中style和theme的基本使用,這裏就不再贅述了,可以查看Dev Guide上的東東,這裏主要說說自己比較困惑的一些部分。

Android platform已經提供了許多的style和theme供開發者使用,可以在R.style類中找到可供使用的style,不過需要把其中的下劃線(_)改成點號(.). 如果我們查看R.style類的文檔,發現有些style沒有描述或者描述的不怎麼清楚,還是看看原文件中怎麼定義的吧。style和themem的原文件可以在<ANDROID_SDK_HOME>/platforms/android-15/data/res/values/下面找到,其中styles.xml和theme.xml文件就是R.style類的定義文件。可以看到theme.xml中定義了Theme.Holo, Theme.Holo.Light等theme。

在values文件下還有一個文件就是attrs.xml,這是R.attr和R.styleable類的定義文件。attrs.xml中定義了每個view的可用的屬性,例如使用android:textAppearance就是在attrs.xml中定義了<attr name="textAppearance" format="reference" />,那麼這裏點format="reference"使什麼意思呢?這裏解釋一下,一些例子來源於網絡:


1. reference:參考某一資源ID。


    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "background" format = "reference" />

            </declare-styleable>

    (2)屬性使用:

             <ImageView

                     android:layout_width = "42dip"

                     android:layout_height = "42dip"

                     android:background = "@drawable/圖片ID"

                     />

2. color:顏色值


           <declare-styleable name = "名稱">

                   <attr name = "textColor" format = "color" />

            </declare-styleable>

3. boolean:布爾值


            <declare-styleable name = "名稱">

                   <attr name = "focusable" format = "boolean" />

            </declare-styleable>

4. dimension:尺寸值。


            <declare-styleable name = "名稱">

                   <attr name = "layout_width" format = "dimension" />

            </declare-styleable>

5. float:浮點值。

6. integer:整型值。

7. string:字符串

8. fraction:百分數。

9. enum:枚舉值

10. flag:位或運算


注意:

     屬性定義時可以指定多種類型值。

    (1)屬性定義:

            <declare-styleable name = "名稱">

                   <attr name = "background" format = "reference|color" />

            </declare-styleable>

    (2)屬性使用:

             <ImageView

                     android:layout_width = "42dip"

                     android:layout_height = "42dip"

                     android:background = "@drawable/圖片ID|#00FF00"

                     />


ok, 瞭解完這些,下面說說怎麼自定義View和屬性,可以參考http://blog.csdn.net/jincf2011/article/details/6344678



xml 文件裏定義控件的屬性,我們已經習慣了android:attrs="" ,那麼我們能不能定義自己的屬性能,比如:test:attrs="" 呢?答案是肯定的.

進入主題。大致以下步驟:

一、res/values 文件下定義一個attrs.xml 文件.代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <declare-styleable name="MyView"
  4.         <attr name="textColor" format="color" /> 
  5.         <attr name="textSize" format="dimension" /> 
  6.     </declare-styleable> 
  7. </resources> 

二、 我們在MyView.java 代碼編寫如下,其中下面的構造方法是重點,我們獲取定義的屬性R.sytleable.MyView_textColor, 獲取方法中後面通常設定默認值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ) 防止我們在xml 文件中沒有定義.從而使用默認值!

MyView 就是定義在<declare-styleable name="MyView "></declare-styleable> 裏的 名字,獲取裏面屬性用 名字_ 屬性 連接起來就可以.TypedArray 通常最後調用 .recycle() 方法,爲了保持以後使用該屬性一致性!


  1. public MyView(Context context,AttributeSet attrs) 
  2.     { 
  3.         super(context,attrs); 
  4.         mPaint = new Paint(); 
  5.          
  6.         TypedArray a = context.obtainStyledAttributes(attrs, 
  7.                 R.styleable.MyView); 
  8.          
  9.         int textColor = a.getColor(R.styleable.MyView_textColor, 
  10.                 0XFFFFFFFF); 
  11.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 
  12.          
  13.         mPaint.setTextSize(textSize); 
  14.         mPaint.setColor(textColor); 
  15.          
  16.         a.recycle(); 
  17.     } 

MyView.java  MyView控件全部代碼如下:


  1. package com.android.tutor; 
  2. import android.content.Context; 
  3. import android.content.res.TypedArray; 
  4. import android.graphics.Canvas; 
  5. import android.graphics.Color; 
  6. import android.graphics.Paint; 
  7. import android.graphics.Rect; 
  8. import android.graphics.Paint.Style; 
  9. import android.util.AttributeSet; 
  10. import android.view.View; 
  11. public class MyView extends View { 
  12.     private Paint mPaint; 
  13.     private Context mContext; 
  14.     private static final String mString = "Welcome to Mr Wei's blog"
  15.      
  16.     public MyView(Context context) { 
  17.         super(context); 
  18.         mPaint = new Paint(); 
  19.     } 
  20.     public MyView(Context context,AttributeSet attrs) 
  21.     { 
  22.         super(context,attrs); 
  23.         mPaint = new Paint(); 
  24.          
  25.         TypedArray a = context.obtainStyledAttributes(attrs, 
  26.                 R.styleable.MyView); 
  27.          
  28.         int textColor = a.getColor(R.styleable.MyView_textColor, 
  29.                 0XFFFFFFFF); 
  30.         float textSize = a.getDimension(R.styleable.MyView_textSize, 36); 
  31.          
  32.         mPaint.setTextSize(textSize); 
  33.         mPaint.setColor(textColor); 
  34.          
  35.         a.recycle(); 
  36.     } 
  37.     @Override 
  38.     protected void onDraw(Canvas canvas) { 
  39.         // TODO Auto-generated method stub 
  40.         super.onDraw(canvas); 
  41.         //設置填充 
  42.         mPaint.setStyle(Style.FILL); 
  43.          
  44.         //畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標 
  45.         canvas.drawRect(new Rect(10, 10, 100, 100), mPaint); 
  46.          
  47.         mPaint.setColor(Color.BLUE); 
  48.         //繪製文字 
  49.         canvas.drawText(mString, 10, 110, mPaint); 
  50.     } 

三、將我們自定義的MyView 加入佈局main.xml 文件中,並且使用自定義屬性,自定義屬性必須加上:

    " xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor"  ,test是自定義屬性的前綴,           com.android.tutor 是我們包名.


main.xml 全部代碼如下:


  1. <?xml  
  2. version="1.0" encoding="utf-8"?> 
  3. <LinearLayout  
  4. xmlns:android="http://schemas.android.com/apk/res/android" 
  5.                
  6. xmlns:test="http://schemas.android.com/apk/res/com.android.tutor" 
  7.     android:orientation="vertical" 
  8.     android:layout_width="fill_parent" 
  9.     android:layout_height="fill_parent" 
  10.     > 
  11. <TextView   
  12.     android:layout_width="fill_parent"  
  13.     android:layout_height="wrap_content"  
  14.     android:text="@string/hello" 
  15.     /> 
  16. <com.android.tutor.MyView 
  17.     android:layout_width="fill_parent"  
  18.     android:layout_height="fill_parent"  
  19.     test:textSize="20px" 
  20.     test:textColor="#fff" 
  21. /> 
  22. </LinearLayout> 


四、運行之效果如下圖:


最後:

關於在引用資源時使用@還是?的問題,我們在設置style的時候既可以使用@也可以使用?, 例如android:textAppearance="@andorid:style/TextAppearance.Medium",

android:textAppearance="?android:attr/textAppearanceMedium"

使用@表示使用固定的style,而不會跟隨Theme改變,這個style可以在style.xml中找到。

而?表示從Theme中查找引用的資源名,例如上面的textAppearanceMedium,查看themes.xml文件,可以看到在不同的theme中,textAppearanceMedium引用的style是不同的。如在Them.Holo中<item name="textAppearanceMedium">@android:style/TextAppearance.Holo.Medium</item>

,Theme.Holo.Light中爲<item name="textAppearanceMedium">@android:style/TextAppearance.Holo.Light.Medium</item>

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