Android自定義View中attrs.xml的實例詳解
Android自定義View中attrs.xml的實例詳解
我們在自定義View的時候通常需要先完成attrs.xml文件
在values中定義一個attrs.xml 然后添加相關屬性
這一篇先詳細介紹一下attrs.xml的屬性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
//自定義屬性名,定義公共屬性
<attr name="titleText" format="string"/>
<attr name="titleTextSize" format="dimension"/>
<attr name="titleTextColor" format="color"/>
<attr name="image" format="reference"/>
<attr name="imageScaleType" >
<enum name="fillXY" value="0"/>
<enum name="center" value="1"/>
</attr>
//自定義控件的主題樣式
<declare-styleable name="CustomImageView">
<attr name="titleText" />
<attr name="titleTextSize" />
<attr name="titleTextColor" />
<attr name="image" />
<attr name="imageScaleType" />
</declare-styleable>
</resources>
reference:參考某一資源ID。
定義:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
</declare-styleable>
使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID"
/>
color:顏色值
定義:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
boolean:布爾值
定義:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"/>
dimension:尺寸值
定義:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
float:浮點值
定義:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
integer:整型值
定義:
<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>
使用:
<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"
/>
enum:枚舉值
定義:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
flag:位或運算
<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>
lt;/declare-styleable>
使用:
<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>
屬性定義時可以指定多種類型值
定義:
<declare-styleable name = "名稱"> <attr name = "background" format = "reference|color" /> </declare-styleable>
使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/圖片ID|#00FF00"
/>
以上就是關于Android 自定義 View 對attrs.xml的詳細介紹,如有疑問請留言或者到本站社區(qū)交流,共同 進步,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
android中可以通過兩種方式調(diào)用接口發(fā)送短信
調(diào)用系統(tǒng)短信接口直接發(fā)送短信;調(diào)起系統(tǒng)發(fā)短信功能,本文將給出兩種方式的實現(xiàn)代碼,感興趣的朋友可以了解下,或許對你有所幫助2013-02-02
Android binder 匿名服務實現(xiàn)雙向通信的解決方案
這篇文章主要介紹了Android binder 匿名服務實現(xiàn)雙向通信的解決方案,當然,這種方案是可行的,只是需要client和server都向servicemanager注冊一個服務,實現(xiàn)起來有點麻煩,不太建議這么做,需要的朋友可以參考下2024-04-04
Android編程使用pull方式解析xml格式文件的方法詳解
這篇文章主要介紹了Android編程使用pull方式解析xml格式文件的方法,結(jié)合實例形式分析了Android調(diào)用pull解析器操作xml格式文件的步驟與相關操作技巧,需要的朋友可以參考下2017-07-07
Android編程實現(xiàn)設置按鈕背景透明與半透明及圖片背景透明的方法
這篇文章主要介紹了Android編程實現(xiàn)設置按鈕背景透明與半透明及圖片背景透明的方法,結(jié)合實例形式較為詳細的分析了Button及ImageButton的背景屬性設置技巧,非常簡單實用,需要的朋友可以參考下2015-12-12
Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能
這篇文章主要為大家詳細介紹了Android實現(xiàn)動態(tài)自動匹配輸入內(nèi)容功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
Android App在線程中創(chuàng)建handler的方法講解
這篇文章主要介紹了Android App在線程中創(chuàng)建handler的方法講解,文中同時講解了handler和線程的關系以及使用Handler時一些需要注意的地方,需要的朋友可以參考下2016-03-03
Android運行時權(quán)限終極方案(PermissionX)
這篇文章主要介紹了Android運行時權(quán)限終極方案(PermissionX),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-05-05

