均屬于筆記鳄炉,僅供個人參考震肮,有問題歡迎指正
Android自定義屬性,屬性類型format詳解
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:百分?jǐn)?shù)。
? ? (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結(jié)合使用秒咨,即是自定義的屬性就是為自定義的控件服務(wù)的喇辽。而自定義樣式或主題是將項目中常用的系統(tǒng)屬性抽取出來,減少代碼的重復(fù)雨席。樣式用于view主題用于Activity或Application菩咨。
一,自定義屬性時命名空間中res與res-auto的區(qū)別
通常我們在布局文件中使用自定義屬性的時候會這樣寫xmlns:app="http://schemas.android.com/apk/res/包路徑"但如果你當(dāng)前工程是做為lib使用舅世,那么你如上所寫 旦委,會出現(xiàn)找不到自定義屬性的錯誤 奇徒。這時候你就必須 寫成xmlns:app="http://schemas.android.com/apk/res-auto/包路徑"
Android 可以自定義View雏亚,同時我們也可以為我們的自定義的View添加自定義屬性俗或,對系統(tǒng)的控件實現(xiàn)擴(kuò)展铡恕,使用方式如同系統(tǒng)控件在xml布局文件中的使用形式涌献。
擴(kuò)展方式:自定義屬性两入,然后再布局文件中使用這些屬性氛堕,在自定義View中獲取這些自定義屬性的值巍实。
具體方式如下:
1.定義屬性:在res/values目錄下創(chuàng)建attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>?
<resources>?
? ? <declare-styleable name="RoundAngleImageView">?
? ? ? ? <attr name="roundWidth" format="dimension" />?
? ? ? ? <attr name="roundHeight" format="dimension" />?
? ? </declare-styleable>?
</resources>?
其中name為該屬性集的名字尼斧,在第三部獲取屬性值時roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);需要在每個參數(shù)名前面添加這個name和下劃線(如:RoundAngleImageView_roundWidth)竣稽。
屬性的類型即format有string , integer , dimension , reference , color , enum.长踊,如果可以同時使用不同的類型功舀。用”|“分割。
枚舉類型的設(shè)置略有不同
<attr name="flowDirection" format="enum">?
? ? ? ? ? <enum name="leftToRight" value="0" />?
? ? ? ? ? <enum name="topDown" value="1" />?
? ? ? ? ? <enum name="rightToLeft" value="2" />?
? ? ? ? ? <enum name="bottomUp" value="3" />?
? ? ? </attr>?
2.在布局文件中使用屬性
<?xml version="1.0" encoding="utf-8"?>?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"?
? ? xmlns:app="http://schemas.android.com/apk/res-auto"?
? ? android:orientation="vertical"?
? ? android:layout_width="fill_parent"?
? ? android:layout_height="fill_parent"?
? ? >?
? ? <com.liupan.app.RoundImage android:id="@+id/roundImage"?
? ? ? ? android:layout_width="fill_parent"?
? ? ? ? android:layout_height="wrap_content"?
? ? ? ? android:layout_alignParentBottom="true"?
? ? ? ? android:background="@drawable/control_bar"?
? ? ? ? android:gravity="center"?
? ? ? ? app:roundWidth = "5dp"?
? ? ? ? app:roundHeight="10dp"/>?
</RelativeLayout>?
首先需要
xmlns:app="http://schemas.android.com/apk/res-auto"?
xmlns后邊的app這個名字可以任意取身弊,屬性設(shè)置就是使用這個名字開頭辟汰。
3. 在自定義組件中的構(gòu)造函數(shù)中獲取設(shè)置的屬性值
public RoundCornerImageView(Context context) {?
? ? ? super(context);?
? ? ? init(context, null);?
? }?
? public RoundCornerImageView(Context context, AttributeSet attrs) {?
? ? ? super(context, attrs);?
? ? ? init(context, attrs);?
? }?
? public RoundCornerImageView(Context context, AttributeSet attrs, int defStyle) {?
? ? ? super(context, attrs, defStyle);?
? ? ? init(context, attrs);?
? }?
第一個構(gòu)造函數(shù)沒有AttributeSet參數(shù),我們需要設(shè)置默認(rèn)值阱佛。
在獲取屬性值得時候帖汞,需要設(shè)置默認(rèn)值,在用戶沒有設(shè)置本屬性時凑术,使用默認(rèn)值翩蘸。
private void init(Context context, AttributeSet attributeSet) {?
? ? ? if (attributeSet != null) {?
? ? ? ? ? TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.RoundAngleImageView);?
? ? ? ? ? assert a != null;?
? ? ? ? ? roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);?
? ? ? ? ? roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);?
? ? ? } else {?
? ? ? ? ? float density = context.getResources().getDisplayMetrics().density;?
? ? ? ? ? roundWidth = (int) (roundWidth * density);?
? ? ? ? ? roundHeight = (int) (roundHeight * density);?
? ? ? }?
? ? ? paint = new Paint();?
? ? ? paint.setColor(Color.WHITE);?
? ? ? paint.setAntiAlias(true);?
? ? ? paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));?
? ? ? paint2 = new Paint();?
? ? ? paint2.setXfermode(null);?
? ? ? a.recycle();//為了保持以后使用的一致性,需要回收?
注意最后調(diào)用TypedArray的recycle方法淮逊。
以上三步催首,搞定扶踊。
參考:http://blog.csdn.net/caesardadi/article/details/20387645
二,Android 自定義主題和風(fēng)格
樣式和主題也是可以繼承和引用的郎任,類似于方法的引用姻檀。
樣式的繼承的格式:<style name="MyText" parent="@android:style/TextAppearance">? ?
引用的格式:? <item name="android:windowTitleStyle">@style/WindowTitle</item> 在某個style中的<item>中引用其他的style。
風(fēng)格涝滴,也就是style绣版,我們可以將一些統(tǒng)一的屬性拿出來,比方說歼疮,長杂抽,寬,字體大小韩脏,字體顏色等等缩麸。
可以在res/values目錄下新建一個styles.xml的文件,在這個文件里面有resource根節(jié)點赡矢,在根節(jié)點里面添加item項杭朱,item項的名字就是屬性的名字,item項的值就是屬性的值吹散,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <style name="MyText" parent="@android:style/TextAppearance">
? ? ? ? <item name="android:textColor">#987456</item>
? ? ? ? <item name="android:textSize">24sp</item>
? ? </style>
</resources>
style中有一個父類屬性parent弧械, 這個屬性是說明當(dāng)前的這個style是繼承自那個style的
<EditText
? ? ? android:id="@+id/myEditText"
? ? ? android:layout_width="match_parent"
? ? ? android:layout_height="match_parent"
? ? ? style="@style/MyText"? ? ? ? ? ? //引用自定義的樣式
? ? ? android:text="測試一下下"/>
說完了style,下面就說說Theme空民,
Theme跟style差不多刃唐,但是Theme是應(yīng)用在Application或者Activity里面的,而Style是應(yīng)用在某一個View里面的界轩,還是有區(qū)別的画饥。 //需要注意的地方
<?xml version="1.0" encoding="utf-8"?>
<resources>
? ? <style name="MyText" parent="@android:style/TextAppearance">
? ? ? ? <item name="android:textColor">#987456</item>
? ? ? ? <item name="android:textSize">24sp</item>
? ? </style>
? ? <style parent="@android:style/Theme" name="CustomTheme">
? ? ? ? <item name="android:windowNoTitle">true</item>
? ? ? ? <item name="android:windowFrame">@drawable/icon</item>
? ? ? ? <item name="android:windowBackground">?android:windowFrame</item>
? ? </style>
</resources>
可以看到這里寫了一個繼承自系統(tǒng)默認(rèn)的Theme的主題,里面有3個屬性浊猾,這里強調(diào)一下第三個屬性的值的問題抖甘,
這里打個問號,然后加前面的一個item的名字表示引用的是那個名字的值葫慎,也就是那個名字對應(yīng)的圖片衔彻。
然后我們在Manifest.xml里面的Application里面加一個Theme的屬性,這個屬性對應(yīng)的就是我們上面寫的Them幅疼。
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/CustomTheme"> //在Manifest.xml中引用自定義的主題
當(dāng)然也可以在代碼中設(shè)置主題:
@Override
? ? protected void onCreate (Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
setTheme(R.style.CustomTheme); //在代碼中引用自定義的主題
setContentView(R.layout.test_style);
? ? }
盡管在定義上米奸,樣式和主題基本相同,但是它們使用的地方不同爽篷。樣式用在單獨的 View 悴晰,如: EditText 、TextView 等;主題通過 AndroidManifest.xml 中的 <application> 和 <activity> 用在整個應(yīng)用或者某個 Activity铡溪,主題對整個應(yīng)用或某個 Activity 進(jìn)行全局性影響漂辐。如果一個應(yīng)用使用了主題,同時應(yīng)用下的 view 也使用了樣式棕硫,那么當(dāng)主題與樣式屬性發(fā)生沖突時髓涯,樣式的優(yōu)先級高于主題。
參考:http://www.cnblogs.com/shang53880/archive/2011/06/20/2085102.html
http://blog.csdn.net/piglite/article/details/46510745
三哈扮, 如何在android style文件中使用自定義屬性
前幾天我在項目中遇到了這樣一個問題:我為項目編寫了一個自定義控件纬纪,這個控件會被大量復(fù)用,所以我準(zhǔn)備在style.xml文件中定義一個style來減少重復(fù)xml布局內(nèi)容的編寫滑肉,但是里面有一個自定義的控件屬性包各,問題出現(xiàn)在這里,雖然自定義屬性在layout布局xml中可以使用正常靶庙,但是卻無法在style中定義问畅,本來這個控件是大量服用的,style也是為了復(fù)用減少xml內(nèi)容寫的六荒,我可以把自定義屬性內(nèi)容直接寫死在自定義控件中护姆,但是考慮到項目未來可能出現(xiàn)的變數(shù),我還是準(zhǔn)備將這個自定義屬性寫到style中掏击,這樣即便有其他不同樣式的復(fù)用卵皂,我也只需再寫一個style即可。
在layout布局中使用自定義屬性是非常簡單铐料,我們只需要在xml根節(jié)點xmlns:Android="http://schemas.android.com/apk/res/android" 的后面加上我們自定義控件的命名空間(栗子:xmlns:test="http://schemas.android.com/apk/res/packagename)渐裂,然后就可以在自定義控件節(jié)點里自由使用自定義屬性了豺旬。
我在編寫style的時候钠惩,起初也以為style.xml也應(yīng)該是這種使用方式,于是就在根節(jié)點添加了自定義控件的命名空間族阅,然后就在style中開始使用自定義屬性篓跛,如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android"?
? ? xmlns:test="http://schemas.android.com/res/com.zhufuing" >?
? ? <style name="test" >?
? ? ? ? <item name="test:name_text">hello,world!</item>? ? ? ?
? ? </style>?
</resources>?
但是這樣并不起作用,style.xml反而出現(xiàn)錯誤坦刀,報錯提示如下:
error: Error: No resource found that matches the given name: attr 'test:name_text'.
我在網(wǎng)上搜索之后得到了正確的答案愧沟,其實我們在style.xml中使用自定義屬性的話,不需要寫自定義控件的命名空間鲤遥,我們只需要在style中使用命名控件的地方換成自定義控件的包名即可(注意:是包名沐寺,不帶自定義控件的名字),如下:
<resources xmlns:android="http://schemas.android.com/apk/res/android" >?
? ? <style name="test" >?
? ? ? ? <item name="com.zhufuing:name_text">hello,world!</item>? ? ? ? //自定義控件的包名
? ? </style>?
</resources>?
這樣就可以在style文件中使用自定義屬性了盖奈。
參考:http://blog.csdn.net/dl6655/article/details/47334087
http://blog.csdn.net/waidazhengzhao/article/details/45771503? ? 講解的很全面詳細(xì)