从Android Launcher源码学习自定义标签【转】

本文详细介绍了如何在Android中自定义View属性的过程,包括在attrs.xml文件中定义样式和属性名称,通过CellLayout.java实现对自定义属性的读取及应用,并展示了具体的XML配置实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

res/values/attrs.xml <declare-styleable name="CellLayout"><!-- The width of a single cell --><attr name="cellWidth" format="dimension"></attr><!-- The height of a single cell --><attr name="cellHeight" format="dimension"></attr> ..... </declare-styleable> res/layout-port/workspace_screen.xml <com.android.launcher.celllayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher" android:layout_width="fill_parent" android:layout_height="fill_parent" launcher:cellwidth="80dip" launcher:cellheight="96dip" ....></com.android.launcher.celllayout> src/com.android.launcher.CellLayout.java public CellLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0); mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 10); mCellHeight = a.getDimensionPixelSize(R.styleable.CellLayout_cellHeight, 10); ..... } 根据颜色看, 应该很清晰: 1. 蓝色部分是自己定义的styleable和attr的名称, 代码和XML里要一致; 2. 绿色部分是自定义的命名空间名称, 也只要上下保持一致即可; 3. 红色部分是View所在的包名, 不允许有误. 参考:http://blog.youkuaiyun.com/Android_Tutor/archive/2010/04/21/5508615.aspx http://hi.baidu.com/lck0502/blog/item/8d4e453f80cf43cf7c1e714b.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值