本文轉自: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 文件.代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <declare-styleable name="MyView">
- <attr name="textColor" format="color" />
- <attr name="textSize" format="dimension" />
- </declare-styleable>
- </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() 方法,爲了保持以後使用該屬性一致性!
- public MyView(Context context,AttributeSet attrs)
- {
- super(context,attrs);
- mPaint = new Paint();
- TypedArray a = context.obtainStyledAttributes(attrs,
- R.styleable.MyView);
- int textColor = a.getColor(R.styleable.MyView_textColor,
- 0XFFFFFFFF);
- float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
- mPaint.setTextSize(textSize);
- mPaint.setColor(textColor);
- a.recycle();
- }
MyView.java MyView控件全部代碼如下:
- package com.android.tutor;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.graphics.Paint.Style;
- import android.util.AttributeSet;
- import android.view.View;
- public class MyView extends View {
- private Paint mPaint;
- private Context mContext;
- private static final String mString = "Welcome to Mr Wei's blog";
- public MyView(Context context) {
- super(context);
- mPaint = new Paint();
- }
- public MyView(Context context,AttributeSet attrs)
- {
- super(context,attrs);
- mPaint = new Paint();
- TypedArray a = context.obtainStyledAttributes(attrs,
- R.styleable.MyView);
- int textColor = a.getColor(R.styleable.MyView_textColor,
- 0XFFFFFFFF);
- float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
- mPaint.setTextSize(textSize);
- mPaint.setColor(textColor);
- a.recycle();
- }
- @Override
- protected void onDraw(Canvas canvas) {
- // TODO Auto-generated method stub
- super.onDraw(canvas);
- //設置填充
- mPaint.setStyle(Style.FILL);
- //畫一個矩形,前倆個是矩形左上角座標,後面倆個是右下角座標
- canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
- mPaint.setColor(Color.BLUE);
- //繪製文字
- canvas.drawText(mString, 10, 110, mPaint);
- }
- }
三、將我們自定義的MyView 加入佈局main.xml 文件中,並且使用自定義屬性,自定義屬性必須加上:
" xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor" ,test是自定義屬性的前綴, com.android.tutor 是我們包名.
main.xml 全部代碼如下:
- <?xml
- version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- <com.android.tutor.MyView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- test:textSize="20px"
- test:textColor="#fff"
- />
- </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>