1. 程式人生 > >android中怎麼在View構造的attrs中拿到android給的屬性以及attrs屬性介紹

android中怎麼在View構造的attrs中拿到android給的屬性以及attrs屬性介紹

attrs獲得,而自定義的屬性獲得值方式如下,當然原生的也是一樣,只需要把attr name該成系統的。

一、 首先要在res/values目錄下建立一個attrs.xml(名字可以自己定義)的檔案,並在此檔案中增加對控制元件的屬性的定義.其xml檔案如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="my_text_view">
       <attr name="text_size" format="float"></attr>
       <attr name="text_color" format="color"></attr>
       <attr name="text_back_ground" format="color|reference"></attr>

   </declare-styleable>
</resources>

在這裡,需要補充attrs屬性的相關知識,即Attr屬性是如何在XML中定義的,
自定義屬性
的Value值可以有10種類型以及其型別的組合值,其具體使用方法如下: 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:顏色值。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "textColor" format = "color" /> </declare-styleable> (2)屬性使用: <TextView android:layout_width = "42dip" android:layout_height = "42dip" android:textColor = "#00FF00" /> 3. boolean:
布林值
。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "focusable" format = "boolean" /> </declare-styleable> (2)屬性使用: <Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true" /> 4. dimension:尺寸值。 (1)屬性定義: <declare-styleable name = "名稱"> <attr name = "layout_width" format = "dimension" /> </declare-styleable> (2)屬性使用: <Button android:layout_width = "42dip" android:layout_height = "42dip" /> 5. float:浮點值。 (1)屬性定義: <declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable> (2)屬性使用: <alpha android:fromAlpha = "1.0" android:toAlpha = "0.7" /> 6. integer:整型值。 (1)屬性定義: <declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <attr name = "drawable" /> </declare-styleable> (2)屬性使用: <animated-rotate xmlns:android = "
http://schemas.android.com/apk/res/android
" android:drawable = "@drawable/圖片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100" /> 7. string:字串。 (1)屬性定義: <declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable> (2)屬性使用: <com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" /> 8. fraction:百分數。 (1)屬性定義: <declare-styleable name="RotateDrawable"> <attr name = "visible" /> <attr name = "fromDegrees" format = "float" /> <attr name = "toDegrees" format = "float" /> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> <attr name = "drawable" /> </declare-styleable> (2)屬性使用: <rotate xmlns:android = "http://schemas.android.com/apk/res/android"    android:interpolator = "@anim/動畫ID" android:fromDegrees = "0"    android:toDegrees = "360" android:pivotX = "200%" android:pivotY = "300%"    android:duration = "5000" android:repeatMode = "restart" android:repeatCount = "infinite" /> 9. enum:列舉值。 (1)屬性定義: <declare-styleable name="名稱"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> (2)屬性使用: <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </LinearLayout> 10. flag:位或運算。 (1)屬性定義: <declare-styleable name="名稱"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> <flag name = "stateVisible" value = "4" /> <flag name = "stateAlwaysVisible" value = "5" /> <flag name = "adjustUnspecified" value = "0x00" /> <flag name = "adjustResize" value = "0x10" /> <flag name = "adjustPan" value = "0x20" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable> (2)屬性使用: <activity android:name = ".StyleAndThemeActivity" android:label = "@string/app_name" android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> 注意: 屬性定義時可以指定多種型別值。 (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" /> 二、接下來實現自定義View的類,其中下面的構造方法是重點,在程式碼中獲取自定義屬性,其程式碼如下: package com.example.CustomAttr; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Paint; import android.util.AttributeSet; import android.util.TypedValue; import android.widget.TextView; /** * Created with IntelliJ IDEA. * User: zhiwen.nan * Date: 13-9-28 * Time: 下午10:00 * To change this template use File | Settings | File Templates. */ public class CustomTextView extends TextView { private TypedArray mTypedArray; private Paint mPaint; public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); inial(context,attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); inial(context,attrs); } private void inial(Context context,AttributeSet attrs) { TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view); float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ; int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ; int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ; super.setTextColor(textColor); super.setTextSize(textsize); super.setBackgroundColor(bgColor); typedArray.recycle(); } } 三、接下來在XML佈局中引用自定義View控制元件,其XML程式碼如下: ?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.example.CustomAttr.CustomTextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, MainActivity" app:text_size ="20" app:text_color ="#00FF00" app:text_back_ground="#ffffff" /> </LinearLayout> 注意上面XML中程式碼: xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定義的app名稱空間,res後面是應用程式包名,然後可以直接使用app:text_size,等屬性,其值型別要和attrs.xml定義的屬性Value值相對應。 四、總結: 注意該例子中是使用app:text_size = "20 和app:text_color="#00FF00定義TextView的顏色和textView的字型大小,而不是使用系統的屬性android:textsize等。該例子中只是起到拋磚引玉的作用,你可以自定義其他屬性,來實現你想要的自定義View效果。