Android自定义View中attrs.xml的实例详解
程序员文章站
2024-02-03 10:41:16
android自定义view中attrs.xml的实例详解
我们在自定义view的时候通常需要先完成attrs.xml文件
在values中定义一个a...
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的详细介绍,如有疑问请留言或者到本站社区交流,共同 进步,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
推荐阅读
-
Android自定义View中attrs.xml的实例详解
-
实例详解Android自定义ProgressDialog进度条对话框的实现
-
Android 中的两端对齐实例详解
-
Android编程中的消息机制实例详解
-
Android 中IntentFilter的匹配规则实例详解
-
详解 Android中Libgdx使用ShapeRenderer自定义Actor解决无法接收到Touch事件的问题
-
Android自定义View的实现方法实例详解
-
Android Listview中显示不同的视图布局详解及实例代码
-
Android 中 Tweened animation的实例详解
-
Android 中 ActivityLifecycleCallbacks的实例详解