Android 自定义属性时TypedArray的使用
對于自定義屬性,遵循以下幾步,就可以實(shí)現(xiàn):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@color/color_white"android:gravity="center_horizontal"android:orientation="vertical"> <com.demo.RoundCornerProgress android:id="@+id/progressBar"android:layout_width="300dp"android:layout_height="13dp"app:rcMax="100000"app:rcBackgroundColor="#dedede"app:rcProgressColor="#fcc329"app:rcRadius="6.5dp" /></LinearLayout>
com.demo.RoundCornerProgress是自定義的ProgressBar,布局文件中
app:rcMax="100000" app:rcBackgroundColor="#dedede" app:rcProgressColor="#fcc329" app:rcRadius="6.5dp"是自定義的屬性。
app是命名空間,自己可以隨便命名其他名字,用來加在自定義屬性前面。
xmlns:android=”http://schemas.android.com/apk/res/android
聲明xml命名空間。xmlns意思為“xml namespace”.冒號后面是給這個(gè)引用起的別名。
schemas是xml文檔的兩種約束文件其中的一種,規(guī)定了xml中有哪些元素(標(biāo)簽)、元素有哪些屬性及各元素的關(guān)系,當(dāng)然從面向?qū)ο蟮慕嵌壤斫鈙chemas文件可以認(rèn)為它是被約束的xml文檔的“類”或稱為“模板”。
早期或簡單的xml用的是另一種約束,稱為DTD,這東西大家天天都見到。html/xhtml中都存在(早期的html可能沒有),如” html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“。
現(xiàn)在大部分xml文檔的約束都換成schema了,原因是schema本身也是xml,二schema擴(kuò)展性強(qiáng)。
rcMax、rcProgress等就是xml里面自己命名的。
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"><!--進(jìn)度條樣式--><declare-styleable name="RoundCornerProgress"><attr name="rcReverse" format="boolean"/><attr name="rcProgress" format="float"/><attr name="rcMax" format="float"/><attr name="rcSecondaryProgress" format="float"/><attr name="rcBackgroundPadding" format="dimension"/><attr name="rcRadius" format="dimension"/><attr name="rcProgressColor" format="color"/><attr name="rcSecondaryProgressColor" format="color"/><attr name="rcBackgroundColor" format="color"/></declare-styleable> </resources>其中的format的意義和可取的值有以下一些:
- reference:表示引用,參考某一資源ID
(1)屬性定義:
(2)屬性使用:
- color:顏色值
- boolean:布爾值
- dimension:尺寸值。注意,這里如果是dp那就會做像素轉(zhuǎn)換
- float:浮點(diǎn)值。
- integer:整型值。
- string:字符串
- fraction:百分?jǐn)?shù)。
- enum:枚舉值
- flag:是自己定義的,類似于 android:gravity=”top”,就是里面對應(yīng)了自己的屬性值。
- reference|color:顏色的資源文件。 12.reference|boolean:布爾值的資源文件
注意://由于reference是從資源文件中獲取:所以在XML文件中寫這個(gè)屬性的時(shí)候必須personattr:name=”@string/app_name”這種格式,否則會出錯
接著就可以在自定義控件中獲取了
context通過調(diào)用obtainStyledAttributes方法來獲取一個(gè)TypeArray,然后由該TypeArray來對屬性進(jìn)行設(shè)置
obtainStyledAttributes方法有三個(gè),我們最常用的是有一個(gè)參數(shù)的obtainStyledAttributes(int[] attrs),其參數(shù)直接styleable中獲得
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyView);
調(diào)用結(jié)束后務(wù)必調(diào)用recycle()方法,否則這次的設(shè)定會對下次的使用造成影響
在控件構(gòu)造方法中調(diào)用該方法就能獲取到值了
public RoundCornerProgressBar(Context context, AttributeSet attrs) {setup(context, attrs);}AttributeSet中的確保存的是該View聲明的所有的屬性,并且可以通過它去獲取(自定義的)屬性。
public CustomView(Context context, AttributeSet attrs) {super(context, attrs);int count = attrs.getAttributeCount();for (int i = 0; i < count; i++) {String attrName = attrs.getAttributeName(i);String attrVal = attrs.getAttributeValue(i);Log.e(TAG, "attrName = " + attrName + " , attrVal = " + attrVal);}}就能打印出所有屬性。
attrName = layout_width , attrVal = 300.0dip
attrName = layout_height , attrVal = 13.0dip
attrName = app:rcMax , attrVal = 100000.0
總結(jié)
以上是生活随笔為你收集整理的Android 自定义属性时TypedArray的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AndroidStudio Gradle
- 下一篇: Android Handler.remo