標籤:android style blog http io color ar 使用 java
一、 在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控制項全部代碼如下
- 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>
format就是格式,裡面的就是這個屬性對應的格式,下面列出來大致的格式有:
1. reference:參考某一資源ID,以此類推
(1)屬性定義:
(2)屬性使用:
<imageview< p="">
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
2. color:顏色值
3. boolean:布爾值
4. dimension:尺寸值。注意,這裡如果是dp那就會做像素轉換
5. float:浮點值。
6. integer:整型值。
7. string:字串
8. fraction:百分數。
9. enum:枚舉值
10. flag:是自己定義的,類似於 android:gravity="top",就是裡面對應了自己的屬性值。
11. reference|color:顏色的資源檔。 12.reference|boolean:布爾值的資源檔
注意://由於reference是從資源檔中擷取:所以在XML檔案中寫這個屬性的時候必須personattr:name="@string/app_name"這種格式,否則會出錯
Android 中自訂控制項和屬性(attr.xml,declare-styleable,TypedArray)的方法和使用